【问题标题】:Selenium + Python + Chrome: simultaneously add_experimental_option and set desired_capabilitiesSelenium + Python + Chrome:同时添加_experimental_option和设置desired_capabilities
【发布时间】:2019-06-16 02:51:36
【问题描述】:

我正在尝试更改 Chrome 驱动程序的设置,以便它允许我执行以下两项操作:

  1. 它没有弹出窗口 (as discussed here)。
  2. 它允许我更改下载目录和设置 (as discussed here)。

尽管这两种解决方案在孤立的情况下都非常有效,但我尝试将它们结合起来却失败了。下面是两个孤立的部分解决方案。在这里感谢任何帮助。

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

【问题讨论】:

  • 欢迎来到 Stack Overflow :) 我看到您已经有了答案,但为了将来参考,解释您尝试过的内容(实际代码最好)以及它在问题本身。

标签: python selenium webdriver selenium-chromedriver webautomation


【解决方案1】:

您可以将选项转换为所需的功能,并在创建驱动程序期间将其传递给desired_capabilities 参数:

capabilities.update(options.to_capabilities())

希望对你有帮助!

【讨论】:

  • 感谢您的回复!有效。在您的单行代码的帮助下,我使用“代码 1”中的“选项”转换了代码 2 中的“功能”。扩展功能完美运行!还分享以下更新的功能:
  • 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': []}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2017-09-06
  • 2020-07-09
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多