selenium.common.exceptions.WebDriverException:消息:会话未创建异常

selenium.common.exceptions.WebDriverException: Message: session not created exception

我正在尝试在我的 mac 版本 10.12.4 上使用 splinter,因为 selenium 是必需的,我正确安装了所有东西但是当我 运行 我的代码打开一个空白 chrome window 然后我一一收到这两个错误:

第一个错误:

Traceback (most recent call last):
  File "/Users/exepaul/Downloads/pythoncrawling/fix.py", line 14, in <module>
    browser = Browser('chrome')  # open a chrome browser
  File "/anaconda/lib/python3.5/site-packages/splinter/browser.py", line 63, in Browser
    return driver(*args, **kwargs)
  File "/anaconda/lib/python3.5/site-packages/splinter/driver/webdriver/chrome.py", line 34, in __init__
    self.driver = Chrome(chrome_options=options, **kwargs)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 140, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 229, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"6860.1","isDefault":true},"id":1,"name":"","origin":"://"}
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.12.4 x86_64)

第二个错误:

Traceback (most recent call last):
  File "/Users/exepaul/Downloads/pythoncrawling/fix.py", line 14, in <module>
    browser = Browser('chrome')  # open a chrome browser
  File "/anaconda/lib/python3.5/site-packages/splinter/browser.py", line 63, in Browser
    return driver(*args, **kwargs)
  File "/anaconda/lib/python3.5/site-packages/splinter/driver/webdriver/chrome.py", line 34, in __init__
    self.driver = Chrome(chrome_options=options, **kwargs)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 140, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 229, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 295, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 464, in execute
    return self._request(command_info[0], url, body=data)
  File "/anaconda/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 488, in _request
    resp = self._conn.getresponse()
  File "/anaconda/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/anaconda/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/anaconda/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

我搜索并找到了 答案,建议更新您的 chrome 驱动程序,所以我安装了最新的 chrome 驱动程序并更新了 /etc/paths 中的路径,但我仍然是得到同样的错误,然后我试图理解这个错误,我想这可能是因为会话,所以我尝试在 python 中使用请求库创建会话:

import requests
MAX_RETRIES = 20


art="https://www.google.com"
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=MAX_RETRIES)
session.mount('https://', adapter)
session.mount('http://', adapter)
r = session.get(art)

但运气不好,我不知道如何解决这个错误,我的代码是:

from splinter import Browser
import pandas as pd



url="https://www.google.com"


browser = Browser('chrome')
browser.visit(url)

search_bar_xpath = '//*[@id="lst-ib"]'
search_bar = browser.find_by_xpath(search_bar_xpath)[0]
search_bar.fill("Tony_stark is Iron_Man")


search_button_xpath = '//*[@id="tsf"]/div[2]/div[3]/center/input[1]'
search_button = browser.find_by_xpath(search_button_xpath)[0]
search_button.click()


search_results_xpath = '//h3[@class="r"]/a'
search_results = browser.find_by_xpath(search_results_xpath)


scraped_data = []
for search_result in search_results:

    title = search_result.text.encode('utf8')
    link = search_result["href"]
    scraped_data.append((title, link))

df = pd.DataFrame(data=scraped_data, columns=["title", "link"])
df.to_csv("links.csv")

我认为你的 chromedriver 没有更新 请从这里更新 link

https://sites.google.com/a/chromium.org/chromedriver/

我也没有在你的代码中看到这样的 chromedriver 路径

from selenium import webdriver 
driver=webdriver.Chrome('D:\Sankalp_Python\Python36\selenium\chromedriver.exe')

我解决了我的问题。由于很多人面临同样的问题,我想我会分享我的解决方案:

我正在通过 /etc/paths 更新 chromedriver 但是 pycharm 显示的是旧版本的 chrome driver 所以首先我有通过 :

卸载旧的 chromedriver
  1. 删除所有带有 brew uninstall --force chromedriver 的版本。

  2. 安装您可以下载的新版本here

现在它完美运行

P.S :我修复的另一个问题:我在访问 google 时没有使用安全连接,所以我必须使用 https://www.google.com instead of http://www.google.com