close

How to use chrome webdriver in selenium to download files in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to use chrome webdriver in selenium to download files in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to use chrome webdriver in selenium to download files in python?

  1. How to use chrome webdriver in selenium to download files in python?

  2. use options.add_experimental_option("prefs", ...) to config selenium+chrome. But sometimes the config may change by time. The beset way to get newest and workable prefs is to check it in the chromium config dir.
  3. use chrome webdriver in selenium to download files in python

  4. use options.add_experimental_option("prefs", ...) to config selenium+chrome. But sometimes the config may change by time. The beset way to get newest and workable prefs is to check it in the chromium config dir.

Method 1

Try this. Executed on windows

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
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
})

Method 2

Some tips:

  1. chromium and chromedriver should have same version.Typically chromium package should have chromedriver inside, you can find it in the install dir. If you are using ubuntu/debian, execute dpkg -L chromium-chromedriver.
  2. Have a correct Chrome preference config.as Satish said, use options.add_experimental_option("prefs", ...) to config selenium+chrome. But sometimes the config may change by time. The beset way to get newest and workable prefs is to check it in the chromium config dir. For example,
    • Launch a chromium in Xorg desktop
    • Change settings in menu
    • Quit chromium
    • Find out the real settings in ~/.config/chromium/Default/Preferences
    • Read it, pick out the exact options you need.

In my case, the code is:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.gpu = False
options.headless = True
options.add_experimental_option("prefs", {
    "download.default_directory" : "/data/books/chrome/",
    'profile.default_content_setting_values.automatic_downloads': 2,
    })

desired = options.to_capabilities()
desired['loggingPrefs'] = { 'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=desired)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read