【发布时间】:2017-06-15 22:40:30
【问题描述】:
如果我指定 chrome_options,那么它会挂起:
params = {'executable_path': path_to_driver}
if self._chrome_options is not None:
params['chrome_options'] = self._chrome_options
print "# before construct"
self._driver = webdriver.Chrome(**params)
print "# after construct"
因此,消息after_construct 未显示。在chrome_options 我传递了字符串:
user-data-dir=/home/sergzach/.config/google-chrome
因此,Chrome 正在启动并进入我的正常配置文件。但是Python 脚本在构建self._driver 时挂起,我无法继续使用Python 脚本。
如果我没有通过self._chrome_options (None),那么一切正常:Chrome 正在启动并且执行更远(before_construct 和 after_construct 都在打印)。
如果我通过空chrome_options:
webdriver.ChromeOptions()
然后它不会挂起。
已安装的 Chrome 版本:55.0.2883.75(64 位)
webdriver 版本:2.25.426924
操作系统:Ubuntu。
更新
有一个回溯(它在脚本挂起后大约 20 秒内引发):
File "test.py", line 6, in <module>
w.start()
File "/usr/local/lib/python2.7/dist-packages/walker/walker.py", line 164, in start
self._driver = webdriver.Chrome(**params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.2.0-42-generic x86_64
更新
这是因为 Chrome 无法连接到远程调试器。我输出日志:
...
params.update(service_args=['--verbose'])
params.update(service_log_path='/tmp/selenium.log')
...
self._chrome_options.add_argument("--verbose")
...
我看到了reason。但我不知道如何关闭传递给 chrome 驱动程序的选项--remote-debugging-port=xxxx。好的,让我们进一步分析来源。
【问题讨论】:
-
self._chrome_options的值是多少? -
@alecxe 它使用值
webdriver.ChromeOptions()可以正常工作,但如果我使用self._chrome_options.add_argument("user-data-dir=/home/sergzach/.config/google-chrome"),它就会挂起。 -
@alecxe 是的,我也尝试过
chown -R sergzach:sergzach目录。结果是一样的。 -
@alecxe Python 脚本挂起,而不是浏览器。
标签: python python-2.7 selenium selenium-chromedriver