gpt4 book ai didi

python - Selenium + Python + Chrome : simultaneously add_experimental_option and set desired_capabilities

转载 作者:行者123 更新时间:2023-12-04 01:47:17 27 4
gpt4 key购买 nike

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

  • 它没有给我一个弹出窗口( as discussed here )。
  • 它允许我更改下载目录和设置( 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")

    最佳答案

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

    capabilities.update(options.to_capabilities())

    希望对你有帮助!

    关于python - Selenium + Python + Chrome : simultaneously add_experimental_option and set desired_capabilities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311586/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com