Чтобы загрузить файл с помощью Selenium в Python, вы можете использовать несколько методов. Вот несколько часто используемых подходов:
Метод 1: использование метода send_keys
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
# Locate the file input element and send the file path
file_input = driver.find_element_by_xpath("//input[@type='file']")
file_input.send_keys("/path/to/your/file.ext")
Метод 2: использование метода execute_script
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
# Locate the file input element and set its value using JavaScript
file_input = driver.find_element_by_xpath("//input[@type='file']")
driver.execute_script("arguments[0].value = arguments[1];", file_input, "/path/to/your/file.ext")
Метод 3. Использование класса ActionChains
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.example.com")
# Locate the file input element and perform the file upload action
file_input = driver.find_element_by_xpath("//input[@type='file']")
ActionChains(driver).move_to_element(file_input).click().send_keys("/path/to/your/file.ext").perform()
Эти методы позволяют найти элемент ввода файла на веб-странице и указать путь к файлу для его загрузки. Вы можете выбрать метод, соответствующий вашим требованиям.