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

时间:2017-09-27 04:49:14

标签: python python-3.x selenium selenium-webdriver splinter

我正在尝试在我的mac版本10.12.4上使用splinter并且因为selenium是必要的,我安装了一切正确但当我运行我的代码时它打开一个空白的chrome窗口然后我逐个得到这两个错误:

第一个错误:

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

我搜索并发现this回答建议更新你的chromedriver所以我安装了最新的chromedriver并更新/ 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")

2 个答案:

答案 0 :(得分:0)

我认为您的chromedriver未更新请从此链接更新

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

此外,我没有在您的代码中看到chromedriver的路径,如此

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

答案 1 :(得分:0)

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

我正在通过/etc/paths更新chromedriver,但是pycharm显示了旧版本的chrome驱动程序,所以首先我必须通过以下方式卸载旧的chromedriver:

  1. 使用brew uninstall --force chromedriver删除所有版本。

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

  3. 现在它完美无缺

    P.S:我发现了另外一个:我在访问谷歌时没有使用安全连接,因此我必须使用https://www.google.com代替http://www.google.com