硒+;Python+;Chrome:同时添加_实验_选项并设置所需的_功能

硒+;Python+;Chrome:同时添加_实验_选项并设置所需的_功能,python,selenium,webdriver,selenium-chromedriver,webautomation,Python,Selenium,Webdriver,Selenium Chromedriver,Webautomation,我正在尝试更改Chrome驱动程序的设置,以便它允许我执行以下两项操作: 它没有给我一个弹出窗口() 它允许我更改下载目录和设置() 尽管这两种解决方案都是孤立的,但我试图将它们结合起来的尝试却以灾难性的失败而告终。下面是两个独立的部分解决方案。谢谢你的帮助 代码1: ### This version save pdf automatically but has automation popup. from selenium import webdriver import time tim

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

  • 它没有给我一个弹出窗口()
  • 它允许我更改下载目录和设置()
  • 尽管这两种解决方案都是孤立的,但我试图将它们结合起来的尝试却以灾难性的失败而告终。下面是两个独立的部分解决方案。谢谢你的帮助

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

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

    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_外部]:True,'useAutomationExtension':False},'extensions':[],'args':[]}
    欢迎使用Stack Overflow:)我知道您已经有了答案,但为了将来的参考,最好解释一下您尝试了什么(实际代码最好),以及问题本身是如何失败的。