使用代理文件selenium,python初始化chromedriver时出现异常

时间:2018-05-02 10:45:31

标签: python selenium selenium-webdriver webdriver selenium-chromedriver

我想用代理初始化chrome webdriver。我在link中找到了一个解决方案。我确实喜欢这样,但是这样的例外。

   driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/lib/chromium-browser/chromedriver')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.35 (0),platform=Linux 4.4.0-122-generic x86_64)

由于我需要创建链接中描述的文件,这里是我的background.js

var config = {
                mode: "fixed_servers",
                rules: {
                  singleProxy: {
                    scheme: "http",
                    host: "172.241.11.1", //modified it for privacy
                    port: parseInt(29842)
                  },
                  bypassList: ["foobar.com"]
                }
              };

        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "aedqbb01",
                    password: "ac7DMjR"
                }
            };
        }

        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: ["<all_urls>"]},
                    ['blocking']
        ); 

和manifest.json

{
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Chrome Proxy",
            "permissions": [
                "proxy",
                "tabs",
                "unlimitedStorage",
                "storage",
                "<all_urls>",
                "webRequest",
                "webRequestBlocking"
            ],
            "background": {
                "scripts": ["background.js"]
            },
            "minimum_chrome_version":"22.0.0"
        }

这是我的python程序

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
chrome_options = Options()
chrome_options.add_argument("--headless")
#chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0")
chrome_options.add_extension("proxy.zip") #where is manifest.json and background.json 
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/lib/chromium-browser/chromedriver')
driver.get("https://www.google.com")
soup = BeautifulSoup(browser.page_source, 'lxml')

代码在没有代理文件的情况下正常工作。

我在这里失踪了。请纠正我。

1 个答案:

答案 0 :(得分:0)

此错误消息......

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally

...表示 ChromeDriver 无法启动/生成新的 WebBrowser ,即 Chrome浏览器会话。

您的主要问题是您使用的二进制文件之间的版本兼容性,如下所示:

  • 您正在使用 chromedriver = 2.35
  • chromedriver=2.35的发行说明明确提及以下内容:
  

支持 Chrome v62-64

  • 您正在使用 chrome = 65.0
  • ChromeDriver v2.38的发行说明明确提及以下内容:
  

支持 Chrome v65-67

  • 您的 Selenium客户端版本 3.11.0

因此 Selenium Client v3.1.0 ChromeDriver 版本( v2.35 )与之间存在明显的不匹配Chrome浏览器版本( v65.0

解决方案

  • ChromeDriver 升级到当前ChromeDriver v2.38级别。
  • Chrome 版本升级到当前 Chrome v66.x 级别。 (as per ChromeDriver v2.38 release notes
  • 通过 IDE 清理您的项目工作区仅使用所需的依赖项重建项目
  • 使用CCleaner工具清除执行测试套件之前和之后的所有操作系统杂务。
  • 如果您的基本 Web客户端版本太旧,请通过Revo Uninstaller将其卸载并安装最新的GA和已发布的 Web客户端版本。
  • 进行系统重启
  • 执行@Test