Issue
I want to download a txt and pdf files to a specific folder. But it just downloads them on another folder. The website http://demo.automationtesting.in/FileDownload.html. Is there something wrong with the code or I didn't put the correct location of the folder?
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeOptions=Options()
chromeOptions.add_experimental_option("prefs", {"download.default_dictionary": "C:\DownloadedAutomationFiles"})
driver=webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver", chrome_options=chromeOptions)
driver.get("http://demo.automationtesting.in/FileDownload.html")
driver.maximize_window()
driver.find_element_by_id("textbox").send_keys("testing")
driver.find_element_by_id("createTxt").click() #generate file button
driver.find_element_by_id("link-to-download").click() #dowload link
#Download PDF FILE
driver.find_element_by_id("pdfbox").send_keys("testing download text file")
driver.find_element_by_id("createPdf").click() #generate file button
driver.find_element_by_id("pdf-link-to-download").click() #dowload link
time.sleep(2)
driver.close()
Solution
To download the required file within Automation Demo Site to a specific folder using Selenium, ChromeDriver and google-chrome you need to pass the preference "download.default_directory"
along with the value (location of the directory) through add_experimental_option()
and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Data_Files\output_files"
})
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("http://demo.automationtesting.in/FileDownload.html")
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "textbox"))).send_keys("testing")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "createTxt"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "link-to-download"))).click()
Snapshot:
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.