How do I download a file to a specific folder in Selenium?
The file is located in a folder, but I can't seem to find how to download it into the folder using Selenium.
Selenium supports HTMLUnit, which does not support downloading files directly. However, it's fairly easy to simulate a click on a link with the WebDriver. Here is a basic example that clicks a download button and saves the resulting file to a file called "foo.txt":
From selenium import webdriver. Driver = webdriver.Firefox() driver.get("") driver.findelementbyid("download").click()
With open("foo.txt", "wb") as f: driver.get("file:///C:/path/to/file.txt")
driver.switchto.defaultcontent()
driver.pagesource f.write(driver.pagesource)
Driver.close() Here is the code in Python 3.6 using a browser called Chromedriver: driver = webdriver.Chrome() driver.get("") driver.findelementbyid("download").click()
With open("foo.txt", "wb") as f: driver.get("file:///C:/path/to/file.txt")
driver.defaultcontent() driver.write(driver.pagesource)
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...