Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Chrome Options and Capabilities

I'm trying to automatically download a file with selenium. To do so I would like to set the default download directory and disable the download prompt. It doesn't seem to be working and the options I'm passing don't even seem to be registering. Below is a sample of how I create the browser. Does anyone know what is going on?

chromedriver = 'PATH/TO/chromedriver'

download_fp = './testPrismaDownload/'
prefs = {
    "download.prompt_for_download" : False,
    "download.default_directory": download_fp
}

options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/google-chrome-stable'
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-setuid-sandbox')
options.add_experimental_option('prefs', prefs)

# i've tried various combinations of `options`, `chrome_options` (deprecated) and `desired_capabilities`
browser = webdriver.Chrome(options=options, desired_capabilities=options.to_capabilities(), executable_path=chromedriver)

none of my specified options appear in browser.capabilities or browser.desired_capabilities. For example the key for chromeOptions in capabilities is goog:chromeOptions': {'debuggerAddress': 'localhost:42911'}.

When I do download_button.click() the command succeeds but nothing downloads. I've also tried it on my mac laptop without the --headless option and when I click the download button the browser opens the download dialog prompting for download confirmation.

Any help/experience would be greatly appreciated.

Python 3.6.6 :: Anaconda, Inc.

Selenium '3.141.0'

Linux 9725a3ce7b7e 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

like image 605
RSHAP Avatar asked Oct 17 '22 07:10

RSHAP


1 Answers

There has been an issue: https://github.com/SeleniumHQ/selenium/issues/5722

It's easy. Call this enabler function after your switch window for your driver:

 def enable_download_in_headless_chrome(driver, download_dir):
    # add missing support for chrome "send_command"  to selenium webdriver
    driver.command_executor._commands["send_command"] = ("POST",'/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    command_result = driver.execute("send_command", params)

expected_download = 'ur/download/path'
opt = Options()
opt.add_experimental_option("prefs", { \
    'download.default_directory': expected_download,
     'download.prompt_for_download': False,
     'download.directory_upgrade': True,
  })
opt.add_argument('--headless')
opt.add_argument('--window-size=1920,1080');

login_page = "https://www.google.com"
driver = webdriver.Chrome(options=opt)
driver.implicitly_wait(5)
driver.get(login_page)
driver.maximize_window()

#On below click you will be in new tab
scoresheet_tab = driver.find_element_by_xpath("//*[@class='sideNav-item is--scoresheet']").click()

instances = driver.window_handles
driver.switch_to.window(instances[1]) # this is the new browser
#this below function below does all the trick
enable_download_in_headless_chrome(driver, expected_download)
like image 153
Jens Dibbern Avatar answered Nov 15 '22 07:11

Jens Dibbern