没有输入类型但HTML中具有按钮类型时,如何使用python selenium-webdriver上传文件?

时间:2020-01-11 18:35:01

标签: python selenium selenium-webdriver

在没有输入类型而是HTML中具有按钮类型的情况下,如何使用python selenium-webdriver上传文件?

我正在尝试使用Selenium将文件上传到网页,但是HTML类型是按钮而不是输入文件。

下面是HTML代码

enter image description here

按钮看起来像这样

enter image description here

我的代码

browser.find_element_by_class_name("ng-scope").send_keys('C:\\Users\\Desktop\\test.png')

但是在运行代码后,文件没有上传。

请告诉我我要去哪里了?

预先感谢 -M

1 个答案:

答案 0 :(得分:1)

存在一个带有type=file的隐藏输入。要使用Selenium上传文件,您必须将密钥发送到input[type=file]

browser.find_element_by_css_selector(".file-upload-input input[type=file]").send_keys('C:\\Users\\Desktop\\test.png')

使用WebDriverWait等待元素出现在DOM中:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ...

wait = WebDriverWait(driver, 5)

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".file-upload-input input[type=file]"))).send_keys('C:\\Users\\Desktop\\test.png')