How to upload file with selenium (Python)?

SeleniumAutomation TestingTesting Tools

We can upload files with Selenium using Python. This can be done with the help of the send_keys method. First, we shall identify the element which does the task of selecting the file path that has to be uploaded.

This feature is only applied to elements having the type attribute set to file. Also, the tagname of the element should be input. Let us investigate the html code of an element having the above properties.

Example

Code Implementation.

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.maximize_window()

driver.get("https://www.tutorialspoint.com/selenium/selenium_automat
ion_practice.htm")
#to identify element
s = driver.find_element_by_xpath("//input[@type='file']")
#file path specified with send_keys
s.send_keys("C:\\Users\\Pictures\\Logo.jpg")

Output

raja
Published on 28-Dec-2020 13:22:38
Advertisements