【问题标题】:Selenium chromedriver hangs if I specify user-data-dir in Chrome options如果我在 Chrome 选项中指定 user-data-dir,Selenium chromedriver 会挂起
【发布时间】: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_constructafter_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


【解决方案1】:

一次只有一个客户端可以连接到调试器。所以,为了解决这个问题,当我们想要使用调试器进入用户配置文件时——为了避免 chromedriver 在尝试连接到调试器时挂起,我们必须关闭现有的 Chrome 会话(我再分享一次this conversation)。

【讨论】:

  • 啊,老是遇到这个问题,被这些症状认不出来。感谢分享!
【解决方案2】:

我通过首先杀死所有 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()

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2015-04-12
    • 2015-08-23
    • 2018-11-11
    • 2019-01-28
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多