[英]Setup selenium with headless chrome error: WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
I am trying to get selenium running with a headless chrome in the following environment:我试图让 selenium 在以下环境中使用无头 chrome 运行:
I've been reading about it at developer.google.com and followed the recommended Running Selenium with Headless Chrome post.我一直在developer.google.com上阅读它,并遵循推荐的Running Selenium with Headless Chrome post。 I followed the steps in the ipython REPL:
我按照 ipython REPL 中的步骤操作:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = '/usr/local/bin'
options.add_argument('headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(chrome_options=options)
But this results in the following error:但这会导致以下错误:
WebDriverException Traceback (most recent call last)
<ipython-input-6-658404e774ae> in <module>()
----> 1 driver = webdriver.Chrome(chrome_options=options)
/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.pyc in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
79 remote_server_addr=self.service.service_url,
80 keep_alive=keep_alive),
---> 81 desired_capabilities=desired_capabilities)
82 except Exception:
83 self.quit()
/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
155 warnings.warn("Please use FirefoxOptions to set browser profile",
156 DeprecationWarning, stacklevel=2)
--> 157 self.start_session(capabilities, browser_profile)
158 self._switch_to = SwitchTo(self)
159 self._mobile = Mobile(self)
/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in start_session(self, capabilities, browser_profile)
250 parameters = {"capabilities": w3c_caps,
251 "desiredCapabilities": capabilities}
--> 252 response = self.execute(Command.NEW_SESSION, parameters)
253 if 'sessionId' not in response:
254 response = response['value']
/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/local/bin is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
I've been looking around and found this answer.我一直在环顾四周,找到了这个答案。 I've seen there the
'headless'
argument is passed like this: add_argument("--headless")
.我已经看到
'headless'
参数是这样传递的: add_argument("--headless")
。 I've tried to add it with the '--'
but with the same exception as result.我尝试使用
'--'
添加它,但与结果相同。
In this comment the --no-sandbox
argument is recommended as well but adding this argument doesn't change anything in my case neither.在此评论中,也建议使用
--no-sandbox
参数,但在我的情况下,添加此参数也不会改变任何内容。
How can I debug and fix this error?如何调试和修复此错误?
Update based on the answer from Wonka:根据 Wonka 的回答进行更新:
I've changed options.add_argument('headless')
to options.add_argument('--headless')
and options.add_argument('window-size=1200x600')
to options.add_argument('--window-size=1200x600')
with the same error as above.我已将
options.add_argument('headless')
更改为options.add_argument('--headless')
和options.add_argument('window-size=1200x600')
更改为options.add_argument('--window-size=1200x600')
与上述相同的错误。
Then as recommended I tried to set resolution after driver = webdriver.Chrome(chrome_options=options)
but this line already fails.然后按照建议,我尝试在
driver = webdriver.Chrome(chrome_options=options)
之后设置分辨率,但这条线已经失败。
#Pass binary on init object
##options.binary_location = '/usr/local/bin/chromedriver'
options.add_argument('--headless')
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=options, executable_path="/usr/local/bin/chromedriver")
To set resolution I use this, after init driver object要设置分辨率,我在初始化驱动程序 object 之后使用它
driver.set_window_size(1280, 1024)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.