5

I am trying to change settings for Chrome driver so that it allows me to do both of the following:

  1. It doesn't give me a popup (as discussed here).
  2. It allows me to change the download directory and settings (as discussed here).

Although both the solutions work phenomenally in isolation, my attempts to combine them have failed disastrously. Below are the two isolated part solutions. Appreciate any help here.

Code 1:

### This version save pdf automatically but has automation popup.

from selenium import webdriver
import time


timestr = time.strftime("%Y%m")

options = webdriver.ChromeOptions()

prefs = {
"download.default_directory": r"C:\temp\\"+timestr,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}

options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="C://temp//chromedriver.exe",options=options)
driver.get("https://www.tutorialspoint.com/selenium/selenium_tutorial.pdf")

Code 2:

### This version has no automation popup but doesn't save pdf automatically.

from selenium import webdriver
import time


timestr = time.strftime("%Y%m")

capabilities = {
    'browserName': 'chrome',
    'chromeOptions':  {
        'useAutomationExtension': False,
        'forceDevToolsScreenshot': True,
        'args': ['--start-maximized', '--disable-infobars']
    }
}    

driver = webdriver.Chrome(executable_path="C://temp//chromedriver.exe",desired_capabilities=capabilities)
driver.get("https://www.tutorialspoint.com/selenium/selenium_tutorial.pdf")
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
RUN
  • 53
  • 1
  • 4
  • Welcome to Stack Overflow :) I see you already have an answer, but for future reference it would be helpful to explain what you tried (the actual code would be best) and exactly how it failed in the question itself. – Jeff B Jan 22 '19 at 16:44

1 Answers1

3

You can convert options to desired capabilities and pass it to the desired_capabilities parameter during creation of driver:

capabilities.update(options.to_capabilities())

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Thanks for your response! It worked. I converted the 'capabilities' in my Code 2 using the 'options' in my 'code 1' with the help of your one-liner. The expanded capabilities work perfectly! Also sharing the updated capabilities below: – RUN Jan 22 '19 at 16:02
  • `capabilities = { 'browserName': 'chrome', 'version': '', 'platform': 'ANY', 'goog:chromeOptions': { 'useAutomationExtension': False, 'forceDevToolsScreenshot': True, 'prefs': { 'download.default_directory': 'C:\\temp\\201901', 'download.prompt_for_download': False, 'download.directory_upgrade': True, 'plugins.always_open_pdf_externally': True, 'useAutomationExtension': False}, 'extensions': [], 'args': []}}` – RUN Jan 22 '19 at 16:03