I don't know how to specify a file name when downloading files from Selenium using Chrome.

Asked 1 months ago, Updated 1 months ago, 4 views

From Selenium, I was able to automatically operate Google Chrome and click on the CSV download button element to download it.

When I download it, the Save As dialog appears. How do I specify where to save the file name?

I tried using the following site as a reference, but I got an error.

Set default download folder for Chrome driver in Selenium - Qiita

Thank you for your cooperation.

Python code

 from selenium import webdriver

chromeOptions=webdriver.ChromeOptions("chromedriver.exe")
prefix={"download.default_directory": "C:\Users\\****\Desktop"}
chromeOptions.add_experimental_option("prefs",prefs)
# Chromediriver Path
chromedriver="chromedriver.exe"

driver=webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)

Error Messages

File"<ipython-input-1-41dea91d03de>", line 4
    prefix={"download.default_directory": "C:\Users\\****\Desktop"}
                                           ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\UXXXXXXescape

Supplementary information (for example, FW/Tool Version)

Python 3.7.3
Windows 7 Jupiter Notebook

python python3 selenium chromedriver

2022-09-30 14:15

1 Answers

By the way, isn't that exactly what the introductory article says?

Notes
When you set the path to download.default_directory, write the delimiter of the hierarchy as \\ or preceded by r or R which means RAW.ex(r"C:\Users\{username}\Downloads\test code")

At this time, the root hierarchy characters of the drive must have one delimiter ex(C:\).The chromedriver path is separated by a slash (/) and passes executable_path and chrome_options as arguments when instantiating the webdriver.

I think the following message in the error code states that \U at the beginning of the path C:\Users was the beginning of the Unicode hexadecimal 8-digit escape expression, but the error occurred because the hexadecimal 8-digit number did not continue.

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\UXXXXXXescape

Why don't we add RAW meaning r or R before the string, as in the introduction, to make the subsequent \\ one instead of two, or vice versa?

"Also, regarding the original naming, it seems that this article says ""cannot be downloaded as a name"", but ""can be renamed as a downloaded file."""
Selenium give file name when downloading

You cannot specify name of download file through selenium.However, you can download the file, find the latest file in the downloaded folder, and rename as you want.
Note: bowed methods from google searches may have errors.but you get the idea.

You cannot use Selenium to specify a name for the download file. However, you can download the file, find the latest file in the folder you downloaded, and rename it as needed.
Note:Methods borrowed from Google Search may experience errors. But you get ideas.

importos
import shutil
filename = max ([Initial_path+"\\"+f for finos.listdir(Initial_path)], key=os.path.getctime)
shutil.move(filename, os.path.join(Initial_path,r"newfilename.ext"))


2022-09-30 14:15

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.