简体   繁体   English

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

[英]How to upload file using python selenium-webdriver when there is no input type but instead it has a button type in HTML?

How to upload file using python selenium-webdriver when there is no input type but instead it has a button type in HTML?当没有输入类型但它有 HTML 中的按钮类型时,如何使用 python selenium-webdriver 上传文件?

I'm trying to upload a file to a webpage using Selenium but the HTML type is a button not an Input file.我正在尝试使用 Selenium 将文件上传到网页,但 HTML 类型是按钮而不是输入文件。

Below is the HTML Code下面是 HTML 代码

在此处输入图片说明

Button looks like this按钮看起来像这样

在此处输入图片说明

My code我的代码

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

But after I run the code the file is not uploaded.但是在我运行代码后,文件没有上传。

Please advise on where i'm going wrong?请告诉我哪里出错了?

Thanks in advance -M提前致谢 -M

There is a hidden input with type=file .有一个type=file的隐藏输入。 To upload file using Selenium yo have to send keys to input[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')

Use WebDriverWait to wait element to be present in the DOM:使用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM