Shay Lavi
Shay Lavi

Reputation: 103

Can't download file through Selenium chrome webdriver in Python

I'm using Selenium webdriver to scrape a website and download a file from it to a folder. for some reason, if I change the default download path of chrome through ChromeOptions(), when I click() the download link, the file shows failed - download error in the download bar at the bottom of the window. I made sure chrome and chrome driver are up to date but it still doesn't work.

Here is the code i'm using to change the default download path.

from selenium import webdriver

download_path = select_directory()
options = webdriver.ChromeOptions()
preferences = {"download.default_directory": download_path}
options.add_experimental_option("prefs", preferences)
browser = webdriver.Chrome(chrome_options=options)

And here is the code i'm using to download the file itself.

from selenium import webdriver

table = browser.find_element_by_id("tblDocs")
documents_info = table.find_elements_by_class_name("clsTableRowNormal")
for document in documents_info:
    str = document.find_element_by_class_name("clsTableCell").text
    if str.find("תשריט") > -1:
        document.find_element_by_tag_name("img").click()

Does anyone know how I can download the file to a specific directory? I thought of maybe downloading the file to the default folder and moving the file from there to my desired path, but I don't know how to get the default folder through selenium. Any help would be appreciated, thank you!

Upvotes: 1

Views: 2451

Answers (1)

Shay Lavi
Shay Lavi

Reputation: 103

So apparently selenium can't download to a path with a / in it's path string. I had to change select_directory() to return a file path string using only \\. Thanks for the help!

Upvotes: 2

Related Questions