带有ChromeDriver Headless的Python Selenium:无法启动-"DevTools请求失败"

人气:566 发布:2022-10-16 标签: python-2.7 selenium selenium-chromedriver headless

问题描述

我已经在Macbook Pro上运行了它,因此,为了将其传输到服务器以供长期使用,我遇到了麻烦.服务器是Ubuntu 16.04(服务器)和Python 2.7.12.

I've got this running on my Macbook Pro, so in an attempt to transfer it to my server for prolonged use, I have run into a snag. Server is Ubuntu 16.04 (server) and Python 2.7.12.

我使用pip安装了最新版本的硒,以及最新版本的ChromeDriver.我可以从命令行开始(存在GPU错误,但似乎没有引起任何问题.)

I installed the latest version of selenium using pip, and the latest version of ChromeDriver. I can start it fine from the command line (there is a GPU error but it seems not to cause any problems.)

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

However, when I try to start it from within python, using this code:

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的所有初始启动脚本之后:

However, when I start things up, here's what I get in the chromedriver.log (after all the initial startup script for 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中返回该错误消息:

And it continues to spit out that error message for about 60 seconds until it ultimately returns this in 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)

我一生无法解决这个问题,因此我进行了广泛的搜索.

I cannot for the life of me figure out how to fix this, and I have searched extensively.

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

I was under the impression that I didn't need to use any virtual screen emulation if I was using headless, but could that be the issue? (I'm running in an init mode w/o a GUI in Ubuntu.)

此外,我不喜欢在该命令中没有传递--headless以启动chrome-stable ...这是问题吗?

Also, I don't like that there's no --headless being passed in that command to start chrome-stable... is that an issue?

谢谢.

Chromedriver版本(我认为是最新版本):

Chromedriver version (I got what I thought was latest version):

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

$ pip freeze | grep selenium
selenium==3.11.0

Chrome

$ google-chrome --version
Google Chrome 65.0.3325.162

这是显示正在发生什么情况的日志跟踪-它在DevTools故障中循环60秒然后死掉.

Here's the log trace that shows what's going on-- it loops on the DevTools failure for 60 seconds then dies.

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

推荐答案

您的chromedriver对于您的chrome版本已过期.您正在使用chromedriver=2.29.461571,但是与Chrome65兼容的最新版chromedriver是2.37

Your chromedriver is out of date for your version of chrome. You're using chromedriver=2.29.461571 but the latest version of chromedriver, which is compatible with Chrome65 is 2.37

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

You can find the latest chromedriver for your platform here: https://sites.google.com/a/chromium.org/chromedriver/downloads

473