简体   繁体   中英

Selenium + Python + Chrome: simultaneously add_experimental_option and set desired_capabilities

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")

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM