问题
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.
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.)
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)
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
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.
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.)
Also, I don't like that there's no --headless being passed in that command to start chrome-stable... is that an issue?
Thanks.
EDITS:
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.
Selenium
$ pip freeze | grep selenium
selenium==3.11.0
Chrome
$ google-chrome --version
Google Chrome 65.0.3325.162
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
回答1:
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
You can find the latest chromedriver for your platform here: https://sites.google.com/a/chromium.org/chromedriver/downloads
回答2:
The answer was-- use the latest version of chromedriver!
https://sites.google.com/a/chromium.org/chromedriver/downloads
I copy/pasted from what I thought was a up to date script and it was too old a version.
来源:https://stackoverflow.com/questions/49389789/python-selenium-with-chromedriver-headless-fails-to-start-devtools-request-f