Upload file with Selenium in Python

故事扮演 提交于 2019-12-19 09:49:05

问题


Is it possible to upload file attachment with selenium in Python script?


回答1:


If there is a form with file input on the page, I think it's straightforward to fill value in the input and submit the form with python api of selenium. You can find some sample code on the document page




回答2:


It can be done via:

element = driver.find_element_by_name("file")
element.send_keys("/home/pavel/Desktop/949IH3GNHAo.jpg")



回答3:


button = driver.find_element_by_xpath("xpathToYourButton")
button.send_keys("fullPathToFile")

Now if you are in windows path to file uses backslash. To avoid issues use double backslashes! C:\ \Users\ ****\ \Desktop\ \1.jpg without spaces.

PS. I know its a from 4 years ago but I have been trying to figure this out for some time and someone might find this usefull...




回答4:


Python solution to upload a video to YouTube using selenium.

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(5) # Wait up to 5 sec before throwing an error if selenium cannot find the element (!important)
driver.get("https://www.youtube.com/upload")
elem = driver.find_element_by_xpath("//input[@type='file']")
elem.send_keys("C:\\full\\path\to\\video.mp4"); # Window$
#elem.send_keys("/full/path/to/video.mp4"); # Linux

Notes:
1 - Be smart, go slowly but surely;
2 - YouTube max uploads per day is 50, but on the first day is 100;
3 - As of 2019, youtube api is limited to 5 video uploads (◔ _◔)




回答5:


it is quite simple, just record it using IDE. Upload command is working



来源:https://stackoverflow.com/questions/8428102/upload-file-with-selenium-in-python

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