Can we automate file upload in Selenium?
I am trying to write a script in python using selenium webdriver.
So far I have done this. Go to the site. Login into my account. Click 'submit' button. And wait for a few seconds for a link to show up. Click that link. So far it has worked perfectly for me. But now I want to upload a file to my profile. I have looked online but I couldn't find any solution to upload file using selenium. Any ideas?
I have no idea why you wouldn't be able to use the FileUploader. It's a standard html element, and as such will be recognized by Selenium. You may need to make sure the appropriate fields are filled in as well. Otherwise, here is the basic script you would need:
From selenium import webdriver. # Create a new instance of Chrome and navigate to your website. Driver = webdriver.Chrome() driver.get("") # Open file uploader, allow upload. Driver.findelementbyid("emailfileuploadbutton").sendkeys("somefilename.png")
# Wait for page to load. Time.sleep(5) # Close file uploader. Driver.findelementbyid("fileuploadbutton").click()
# Open browser, check if file was uploaded. Driver.get("") if "myfile.png" in driver.title:
print "File was uploaded". Else: print "File was not uploaded". I'm not certain how long the wait time will need to be for this to work. Thats likely to be user dependent.
How to upload file in Selenium using Python?
I am trying to upload a file to a website using Selenium.
Here is my code: from selenium import webdriver. From bs4 import BeautifulSoup. Import time. Import os. # Browser and OS details. Browser = "Mozilla". Os = "Windows 7". # Create a new instance of the Firefox browser. Driver = webdriver.Firefox(executablepath="C:/Users/Pankaj/Documents/test/chromedriver.exe")
Driver.get("") time.sleep(5) # Identify the elements to be clicked. Driver.findelementbyid("loginFormId").sendkeys("email@email.com")
Driver.findelementbyid("loginFormId").sendkeys("password")
# Find the file input element. InputFileElement = driver.findelementbyclassname("input-file") inputFileElement.click() # Specify the path of the file input element. InputFileElement.sendkeys(r"C:UsersPankajDownloadsMyPicture.jpg")
# Send the enter key to move to the next element. InputFileElement.sendkeys(Keys.RETURN)
# Click the Submit button. Driver.click() I am getting this error: Exception in thread "main" org.openqa.NoSuchElementException: no such element: Unable to locate element:
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.33.506120 (e60c724591d75e1b3c1e6c72d0b270692761d3b),platform=Windows NT 10.15063 x8664) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds. Build info: version: '3.141.
Related Answers
How can we use the Selenium tool with HeadSpin?
Selenium is a tool that is used to automate functional testing. There are two types...
What are 5 Uses of Selenium?
Selenium is a web-automation tool that helps you to test web applications....
How can we use the Selenium tool with HeadSpin?
Selenium is a cross-browser testing automation framework w...