【问题标题】:Set chrome.prefs with python binding for selenium in chromedriver在 chromedriver 中使用 python 绑定为 selenium 设置 chrome.prefs
【发布时间】:2013-02-16 10:02:12
【问题描述】:

我整天都在寻找这个,似乎目前没有可从 python 的 chromedriver 实现中获得解决方案。

如何使用 webdriver.Chrome() 方法设置特定的 chrome.prefs(例如 profile.managed_default_content_settings.images = 2 等配置文件设置)?

我已经通过 webdriver.ChromeOptions() 尝试过,但没有成功。在 Java 中,有适当的函数可以实现这一点。

但是 Python 呢?这就是我目前正在做的事情......

    options = webdriver.ChromeOptions()
    options.add_argument('--allow-running-insecure-content')
    options.add_argument('--disable-web-security')
    options.add_argument('--disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache')
    options.add_argument('--no-referrers')
    options.add_argument('--window-size=1003,719')
    options.add_argument('--proxy-server=localhost:8118')
    options.add_argument("'chrome.prefs': {'profile.managed_default_content_settings.images': 2}")


    self.selenium = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',chrome_options=options)

【问题讨论】:

    标签: python selenium selenium-chromedriver


    【解决方案1】:

    对于任何想在 chromedriver 中禁用图像的人,以下代码可能会对您有所帮助。

    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_experimental_option( "prefs", {'profile.default_content_settings.images': 2})
    driver = webdriver.Chrome(chrome_options=chrome_options)
    

    【讨论】:

      【解决方案2】:

      对于遇到此问题的其他人来说只是一个小更新。

      对于较新的版本,以下代码可以正常工作:

      options.add_experimental_option('prefs', {'download.default_directory':'C:\\temp'})
      

      【讨论】:

        【解决方案3】:

        这适用于从至少 2.15 到当前版本 2.20 的最新 chromedriver 版本:

        chrome_options = Options()
        chrome_options.add_experimental_option( "prefs", {'profile.managed_default_content_settings.images': 2})
        chrome = webdriver.Chrome('/path/to/chromedriver',chrome_options=chrome_options)
        chrome.get("https://google.com")
        

        【讨论】:

          【解决方案4】:

          修复:

          有一个解决方案是避免使用 chromeoptions 对象并恢复到所需的功能字典(已弃用)。出于某种原因,selenium 库中的 webdriver.py 将一个空的 chromeoptions 字典添加到了 desiredcapabilities 字典中,这使其无用。所以你需要在 webdriver.py 中取消注释第 54 行

          desired_capabilities.update(options.to_capabilities())
          

          然后使用此代码将所有需要的功能传递给 chromedriver

          CHROME = {
          "browserName": "chrome",
                  "version": "",
                  "platform": "ANY",
                  "javascriptEnabled": True,
                  "chrome.prefs": {"profile.managed_default_content_settings.images": 2},
                  "proxy": {
                      "httpProxy":"localhost:8118",
                      "ftpProxy":None,
                      "sslProxy":None,
                      "noProxy":None,
                      "proxyType":"MANUAL",
                      "class":"org.openqa.selenium.Proxy",
                      "autodetect":False
                      },
                  "chrome.switches": ["window-size=1003,719", "allow-running-insecure-content", "disable-web-security", "disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache", "no-referrers"],
                  }
          
          
              self.selenium = webdriver.Chrome(desired_capabilities=CHROME)
          

          【讨论】:

          • 这里有类似的问题(我正在尝试更改 Chrome 的下载文件夹)。尝试了您的代码,但不知何故它对我不起作用。我的 webdriver.py 文件中的 desired_capabilities.update(options.to_capabilities()) 行没有被注释掉。有什么想法吗?您是否遇到过其他解决方案?
          • 不,就是这样。你找到这条线了吗? desired_capabilities.update(options.to_capabilities())
          • 我做到了。它没有被注释掉,所以它应该可以工作,但不知何故它没有。最后我完全放弃了 Chrome。
          • 谢谢!如果caps['chromeOptions'] 存在,caps['chrome.prefs'] 将被忽略。子类ChromeOptions 并覆盖to_capabilities 使其工作。
          • @zhangyoufu 没错!这是实现它的一种方式!
          【解决方案5】:

          对于任何在 Python 语法上苦苦挣扎的人,这里有一个完整的、有效的示例。它会禁用 Chrome 的“您希望 Google Chrome 保存此站点的密码吗?”迅速的。另见WebDriver Chrome Browser: Avoid 'Do you want chrome to save your password' pop up

          from selenium import webdriver
          from selenium.webdriver.chrome.options import Options
          chrome_options = Options()
          chrome_options.add_experimental_option('prefs', {
              'credentials_enable_service': False,
              'profile': {
                  'password_manager_enabled': False
              }
          })
          driver = webdriver.Chrome(chrome_options=chrome_options)
          driver.get('https://google.com')
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-01-28
            • 1970-01-01
            • 1970-01-01
            • 2018-10-21
            • 2018-07-29
            • 2018-04-11
            • 2016-10-13
            • 2017-03-07
            相关资源
            最近更新 更多