使用ChromeDriver无头的Python Selenium:无法启动 - “DevTools请求失败”

时间:2018-03-20 16:43:10

标签: python-2.7 selenium selenium-chromedriver headless

我已经在我的Macbook Pro上运行了这个,所以试图将它转移到我的服务器上长时间使用,我遇到了麻烦。服务器是Ubuntu 16.04(服务器)和Python 2.7.12。

我使用pip和最新版本的ChromeDriver安装了最新版本的selenium。我可以从命令行启动它(有一个GPU错误,但它似乎不会导致任何问题。)

但是,当我尝试在python中启动它时,使用以下代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

path_to_chromedriver = '/usr/local/bin/chromedriver'
options = Options()
options.add_argument("start-maximized")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
options.add_argument("--no-sandbox") 
service_args = ['--verbose']
service_log_path = '/tmp/local/chromedriver.log'
driver = webdriver.Chrome(executable_path=path_to_chromedriver, chrome_options=options, service_args=service_args, service_log_path=service_log_path)

然而,当我开始时,这是我在chromedriver.log中获得的内容(在COMMAND InitSession的所有初始启动脚本之后:

[0.998][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-ap\
ps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --ign\
ore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.tPkUXa/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basi\
c --remote-debugging-port=12752 --safebrowsing-disable-auto-update --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.org.chromium.Chromium.ODIHcL\
 data:,
[0.998][DEBUG]: DevTools request: http://localhost:12752/json/version
[1.078][DEBUG]: DevTools request failed

它继续吐出该错误消息大约60秒,直到它最终在Python中返回:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 4.4.0-116-generic x86_64)

我不能为我的生活弄清楚如何解决这个问题,而且我已经进行了广泛的搜索。

我的印象是,如果我使用无头,我不需要使用任何虚拟屏幕模拟,但这可能是问题吗? (我在Ubuntu中没有GUI的初始模式下运行。)

另外,我不喜欢没有 - 在该命令中没有通过启动chrome-stable ...这是一个问题吗?

感谢。

编辑:

Chromedriver版本(我得到了我认为的最新版本):

$ chromedriver
Starting ChromeDriver 2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5) on port 9515
Only local connections are allowed.

$ pip freeze | grep selenium
selenium==3.11.0

$ google-chrome --version
Google Chrome 65.0.3325.162

这是显示正在发生的事情的日志跟踪 - 它在DevTools故障上循环60秒然后死亡。

Full log/error trace:
[0.997][INFO]: COMMAND InitSession {
   "capabilities": {
      "alwaysMatch": {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "start-maximized", "--headless", "--disable-gpu", "--disable-extensions", "--no-sandbox" ],
            "extensions": [  ]
         },
         "platformName": "any"
      },
      "firstMatch": [ {

      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "start-maximized", "--headless", "--disable-gpu", "--disable-extensions", "--no-sandbox" ],
         "extensions": [  ]
      },
      "platform": "ANY",
      "version": ""
   }
}
[0.997][INFO]: Populating Preferences file: {
   "alternate_error_pages": {
      "enabled": false
   },
   "autofill": {
      "enabled": false
   },
   "browser": {
      "check_default_browser": false
   },
   "distribution": {
      "import_bookmarks": false,
      "import_history": false,
      "import_search_engine": false,
      "make_chrome_default_for_user": false,
      "show_welcome_page": false,
      "skip_first_run_ui": true
   },
   "dns_prefetching": {
      "enabled": false
   },
   "profile": {
      "content_settings": {
         "pattern_pairs": {
            "https://*,*": {
               "media-stream": {
                  "audio": "Default",
                  "video": "Default"
               }
            }
         }
      },
      "default_content_setting_values": {
         "geolocation": 1
      },
      "default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      },
      "password_manager_enabled": false
   },
   "safebrowsing": {
      "enabled": false
   },
   "search": {
      "suggest_enabled": false
   },
   "translate": {
      "enabled": false
   }
}
[0.997][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[0.998][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --ignore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.tPkUXa/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12752 --safebrowsing-disable-auto-update --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.org.chromium.Chromium.ODIHcL data:,
[0.998][DEBUG]: DevTools request: http://localhost:12752/json/version
[1.078][DEBUG]: DevTools request failed
...
[61.018][DEBUG]: DevTools request: http://localhost:12752/json/version
[61.018][DEBUG]: DevTools request failed
[61.021][INFO]: RESPONSE InitSession unknown error: Chrome failed to start: exited abnormally
[61.021][DEBUG]: Log type 'driver' lost 0 entries on destruction
[61.021][DEBUG]: Log type 'browser' lost 0 entries on destruction

2 个答案:

答案 0 :(得分:2)

您的镀铬机已经过时了您的镀铬版本。您使用的是chromedriver=2.29.461571,但与Chrome65兼容的最新版chromedriver为2.37

您可以在此处找到适用于您平台的最新chromedriver:https://sites.google.com/a/chromium.org/chromedriver/downloads

答案 1 :(得分:0)

答案是 - 使用最新版的chromedriver!

https://sites.google.com/a/chromium.org/chromedriver/downloads

我从我认为是最新的脚本中复制/粘贴,而且版本太旧了。