我正在尝试在以下环境中使用无头铬运行硒:
我已经在developer.google.com上阅读了有关它的内容,并遵循了推荐的“运行Head Selenium with Headless Chrome”帖子。我遵循了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)
但这会导致以下错误:
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.)
我一直在环顾四周,找到了这个答案。我已经看到那里的'headless'
参数是这样传递的:add_argument("--headless")
。我尝试将其添加为,'--'
但结果相同。
在此注释中,--no-sandbox
也建议使用该参数,但是在我看来,添加该参数不会改变任何内容。
如何调试和修复此错误?
根据Wonka的答案进行更新:
我更改options.add_argument('headless')
为options.add_argument('--headless')
和时options.add_argument('window-size=1200x600')
出现options.add_argument('--window-size=1200x600')
与上述相同的错误。
然后按照建议,我尝试在之后设置分辨率,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")
要设置分辨率,我在初始化驱动程序对象之后使用它
driver.set_window_size(1280, 1024)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句