I want to save the file I downloaded from selenium to my desktop.

Asked 2 years ago, Updated 2 years ago, 88 views

Prerequisites/What you want to achieve

I'd like to save the file I downloaded from python selenium to my desktop.
I did exactly what was listed on the ↓ site, but it fell into the Downloads folder of the default download.
https://qiita.com/py_maro/items/6e79e4049677cf43c398
I have left what is written on ↑'s site.

What's wrong with it?
I use Google Chrome.

Problems/Error Messages you are experiencing

There are no specific errors.

Source Codes Affected

# Specify the folder to download to
# Use Cases:
# download_directory='r:\\Users\\<Your home directory>\\Downloads'
# Note: Backslash(\) must be marked with "\\" and escape character
download_directory='r:\\Users\\********\Desktop'

###############################################################
### You can download the file without displaying a dialog.
### The file is saved in the path configured in download_directory.
#
# Use Cases:
#   driver=init_selenium()
#   target_url='www.WannaGetFileFromHere.com'
#   driver.get(target_url)
###############################################################
default_selenium():
    ### Set options to Chrome
    chop=webdriver.ChromeOptions()#
    prefix={"download.default_directory":download_directory}
    chop.add_experimental_option("prefs",prefs)
    chop.add_argument('--ignore-certificate-errors')# SSL error protection
    driver=webdriver.Chrome(chrome_options=chop)
    return driver

Supplementary information (for example, FW/Tool Version)

  • Python 3.7.3
  • Windows 7
  • jupyter notebook
  • chromedriver

python3 selenium

2022-09-29 22:48

1 Answers

You may be confused by the combination of some information, but if it's not a mistake when you ask a question, the drive name is r:.

R:Isn't there a drive?

Isn't this c: or C:?

add

After experimenting, it seems that the following options are required for downloading to the desktop for the prefs configuration.Without this, even if you specify a desktop folder, it will be downloaded to the download folder.
There are two more options specified in the reference article below, but you didn't have to.

"download.directory_upgrade"—True

Note:
How to control the download of files with Selenium + Python bindings in Chrome

The path you decided for the default directory is invalid. Either escape the back slashes or provisional initial string.

options=webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
  "download.default_directory":r "C:\Users\xxx\downloads\Test",
  "download.prompt_for_download": False,
  "download.directory_upgrade"—True,
  "safebrowsing.enabled"—True
})
driver=webdriver.Chrome(chrome_options=options)

Here are the available preferences:
https://cs.chromium.org/chromium/src/chrome/common/pref_names.cc


2022-09-29 22:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.