簡體   English   中英

Selenium + Python + Chrome:同時添加_experimental_option並設置期望的功能

[英]Selenium + Python + Chrome: simultaneously add_experimental_option and set desired_capabilities

我正在嘗試更改Chrome驅動程序的設置,以便它可以執行以下兩項操作:

  1. 它不會給我彈出窗口( 如此處所述 )。
  2. 它允許我更改下載目錄和設置( 如此處所述 )。

盡管這兩種解決方案都顯着地孤立地工作,但是我將它們組合在一起的嘗試卻失敗了。 以下是兩個隔離的零件解決方案。 在這里感謝任何幫助。

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

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

您可以在創建驅動程序的過程desired_capabilities選項轉換為所需的功能,並將其傳遞給desired_capabilities參數:

capabilities.update(options.to_capabilities())

希望對您有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM