如果我指定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位)
网络驱动程序版本: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")
...
我明白原因了。但我不知道如何关闭选项--remote-debugging-port=xxxx
,它正在传递给铬驱动器。好的,让我们进一步分析来源。
一次只能将一个客户端连接到调试器。因此,为了解决这个问题,当我们想使用调试器输入用户配置文件时,为了避免chromedriver在尝试连接到调试器时挂起,我们必须关闭现有的Chrome会话(我再次分享这个对话)。
我通过首先杀死所有的chrome进程来绕过这个问题:
import psutil
for proc in psutil.process_iter():
print(proc)
# Check whether the process name matches or not
if proc.name() == 'chrome' or proc.name() == 'chromedriver':
proc.kill()