【问题标题】:Python/Selenium/Chromedriver: the script opens just a blank Google Chrome pagePython/Selenium/Chromedriver:脚本只打开一个空白的 Google Chrome 页面
【发布时间】:2020-03-23 00:29:00
【问题描述】:

我在特定 Windows 7 机器上的浏览器自动化脚本有问题。代码是用 Selenium 和 Chromedriver 用 python 3.7.4 编写的。当我从命令行运行它时,只有 Chrome 浏览器会启动,但它不会打开 url。这个问题只发生在一台 Windows 7 机器上,我不知道它的原因。我尝试在禁用防火墙和防病毒软件的情况下运行脚本,但不幸的是,这些措施无济于事。命令行中也没有任何错误输出。

我认为某些东西阻止了脚本连接到互联网,但是带有 urllib.request 的 python 脚本运行没有任何问题。

该脚本在 Fedora 30 和 Debian 10 上运行良好。我还通过 Gnome Boxes 在 Windows 10 和 Windows 7 上对其进行了测试:一切正常。

原始代码大约 3000 行,所以这里是我从头开始编写的一个小示例:

from selenium import webdriver

browser = webdriver.Chrome(executable_path = 'webdriver/chromedriver.exe')
print('Starting')
browser.get('https://google.com')

所以当我运行脚本时,除了在 Chrome 中打开一个空白页面外,什么也没有发生。并且“打印”也没有执行。

我已将“浏览器”变量存储在单独的文件中。当我在同一个文件中使用此变量运行脚本时,我收到以下错误消息:

DevTools listening on ws://127.0.0.1:27046/devtools/browser/1ecf2c8f-c0cb-44d7-9
27d-cfa3901f645b
Traceback (most recent call last):
File "test-no-conf.py", line 5, in <module>
executable_path = 'webdriver/chromedriver.exe'
File "C:\Users\К\AppData\Local\Programs\Python\Python37\lib\site-packages\sele
nium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\К\AppData\Local\Programs\Python\Python37\lib\site-packages\sele
nium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\К\AppData\Local\Programs\Python\Python37\lib\site-packages\sele
nium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\К\AppData\Local\Programs\Python\Python37\lib\site-packages\sele
nium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\К\AppData\Local\Programs\Python\Python37\lib\site-packages\sele
nium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not crea
ted
from disconnected: Unable to receive message from renderer
(Session info: chrome=77.0.3865.120)

提前谢谢你。

【问题讨论】:

  • 请分享脚本
  • 请阅读minimal reproducible example并相应地编辑您的问题
  • 谢谢。我在帖子中添加了一个代码示例。这不是我在那些机器上运行的实际代码。但是我写了几个小代码示例(比如那个)来测试脚本是否可以工作,但它无法获取 url。
  • 在打印语句之前打开浏览器对我来说似乎很奇怪。

标签: python selenium selenium-chromedriver


【解决方案1】:

检查你机器上安装的Chrome浏览器版本,并与Chrome驱动版本对比。

您可以通过here了解更多有关这些更改的信息并下载最新的驱动程序here

特别是这些说明:

"以下是选择要下载的 ChromeDriver 版本的步骤: 首先,找出您使用的 Chrome 版本。假设您有 Chrome 72.0.3626.81。

获取 Chrome 版本号,删除最后一部分,并将结果附加到 URL“https://chromedriver.storage.googleapis.com/LATEST_RELEASE_”。例如,对于 Chrome 版本 72.0.3626.81,您将获得一个 URL“https://chromedriver.storage.googleapis.com/LATEST_RELEASE_72.0.3626”。

使用在上一步中创建的 URL 检索包含要使用的 ChromeDriver 版本的小文件。例如,上面的 URL 会得到一个包含“72.0.3626.69”的文件。 (当然,实际数字将来可能会发生变化。)

使用从上一步中检索到的版本号来构建下载 ChromeDriver 的 URL。对于版本 72.0.3626.69,URL 将是“https://chromedriver.storage.googleapis.com/index.html?path=72.0.3626.69/”。

初次下载后,建议偶尔再过一遍上述流程,看看是否有bug修复发布。”

如果这不能解决此错误,请确保关闭所有以前的 chrome 和驱动程序实例。

【讨论】:

  • 谢谢。但不幸的是,它不起作用。 chrome 和 chromedriver 版本都是 77,并且 chrome、chromedriver 和 chromium 的所有进程在启动脚本之前都已终止。我认为这可能是该机器上特定 Windows 7 版本的问题。看来我必须重新安装操作系统。
  • Selenium 模块的版本是多少?
  • 它是 Selenium 3.141.0
  • 那么它应该可以工作。您可以尝试卸载并重新安装 Selenium、Python、Chrome 并查看是否可以解决问题?
  • 我已经试过了,不过还是谢谢你。我想知道是否有任何可能与 Selenium 冲突的系统组件。
【解决方案2】:

我已经重新安装了系统,现在一切正常。那台特定机器上的系统似乎有一些问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多