当我使用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)
有人能帮我吗?谢谢。
此错误消息…
selenium.common.exceptions.SessionNotCreatedException: Message: session not created from disconnected: unable to connect to renderer
…暗示 ChromeDriver 无法启动/产生新的 WebBrowser, 即 Chrome浏览器 会话。
您需要考虑一个事实:
当您使用 Mac OS X时 , 密钥 executable_path必须受以下 值 支持:
executable_path
'/Users/qa/Documents/Python/chromedriver'
因此,行将是:
driver = webdriver.Chrome(executable_path='/Users/qa/Documents/Python/chromedriver')
注意 : 路径 本身是原始路径,因此您无需添加r和删除开关。
r
此外,请确保/etc/hosts您的系统上包含以下条目:
/etc/hosts
127.0.0.1 localhost.localdomain localhost #or 127.0.0.1 localhost loopback