当我使用Python使用Selenium执行此代码时:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
发生错误:
Traceback (most recent call last):
File "/Users/qa/Documents/Python/try.py", line 4, in <module>
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.44.609545 (c2f88692e98ce7233d2df7c724465ecacfe74df5),platform=Mac OS X 10.13.6 x86_64)
有人可以帮助我吗?谢谢。
答案 0 :(得分:1)
此错误消息...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
...表示 ChromeDriver 无法启动/产生新的 WebBrowser ,即 Chrome浏览器会话。
您需要考虑一个事实:
在使用 Mac OS X 时,必须使用 Value 来支持 Key executable_path
: / p>
'/Users/qa/Documents/Python/chromedriver'
因此行将是:
driver = webdriver.Chrome(executable_path='/Users/qa/Documents/Python/chromedriver')
注意:路径本身是原始路径,因此您无需添加开关r
并将其放置。
另外,确保系统上的/etc/hosts
包含以下条目:
127.0.0.1 localhost.localdomain localhost
#or
127.0.0.1 localhost loopback
答案 1 :(得分:1)
在 linux 上遇到同样的问题,特别是 raspberry pi 4(所以是 arm cpu)。我使用了chromium-browser 和chromium-chromedriver 包。
这是我的代码:
browser = Chrome(executable_path = '/usr/bin/chromedriver', options = opts)
这是错误信息:
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/ubuntu/Documents/reliant-scrape/reliant_scrape.py", line 296, in <module>
output = logon(config['headless'], config['download'], config['site'], config['creds'])
File "/home/ubuntu/Documents/reliant-scrape/reliant_scrape.py", line 75, in logon
browser = Chrome(executable_path = '/usr/bin/chromedriver', options = opts)
File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=88.0.4324.150)
然而,当我单独运行 chromedriver
时它工作正常吗?我也将这一行添加到 /etc/hosts
中,但这没有帮助。
答案 2 :(得分:1)
我遇到了类似的错误,首先得到错误信息:
<块引用>selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:正常退出。 (未知错误:DevToolsActivePort 文件不存在)
通过向 ChromeOptions() 添加 options.add_argument("--remote-debugging-port=9230")
解决了这个问题。程序运行一次,我收到了与上面相同的错误消息:
selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建 从断开连接:无法连接到渲染器 (会话信息:无头 chrome=89.0.4389.114)
这里的问题是 chrome 进程没有在程序中正确关闭,因此该进程在调试端口上仍然处于活动状态。要解决此问题,请关闭活动端口 sudo kill -9 $(sudo lsof -t -i:9230)
并简单地将以下几行添加到代码末尾:
driver.stop_client()
driver.close()
driver.quit()
因为我没有在任何地方找到这个答案,我希望它对某人有帮助。
答案 3 :(得分:0)
我在Windows 10计算机上遇到了同样的问题。解决该问题的方法是打开任务管理器并退出所有Python.exe进程以及所有Chrome.exe进程。完成此操作后,