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

大城市里の小女人 提交于 2020-01-30 10:56:38

问题


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

I'm trying to upload a file to a webpage using Selenium but the HTML type is a button not an Input file.

Below is the HTML Code

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


回答1:


There is a hidden input with type=file. To upload file using Selenium yo have to send keys to 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:

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')


来源:https://stackoverflow.com/questions/59697447/how-to-upload-file-using-python-selenium-webdriver-when-there-is-no-input-type-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!