【问题标题】:Chromedriver in Python error SessionNotCreatedExceptionPython 中的 Chrome 驱动程序错误 SessionNotCreatedException
【发布时间】:2021-10-02 04:30:48
【问题描述】:

我正在尝试在 Python 中使用 Selenium 和 Chromedriver。我目前正在使用 PyCharm。我的下载中保存了 chromedriver.exe 文件,它与我正在使用的 Chrome 版本是最新的。

文件路径为“C:\Users\ea.palacios\Downloads\chromedriver.exe'

我的脚本如下:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\\Users\\ea.palacios\\Downloads\\chromedriver.exe')

我也试过删除双反斜杠:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\Users\ea.palacios\Downloads\chromedriver.exe')

当我尝试运行任一脚本时,浏览器会短暂出现不到一秒钟,然后立即关闭。然后 PyCharm 返回以下消息:

Traceback (most recent call last):
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\main.py", line 3, in <module>
    driver = webdriver.Chrome(executable_path='C:\\Users\\ea.palacios\\Downloads\\chromedriver.exe')
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\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 tab crashed
  (Session info: chrome=92.0.4515.107)

这是screenshot of code and error message

这可能是因为我在工作计算机上以管理员身份运行 PyCharm,但 Chrome 驱动程序保存在我的常规下载文件夹中吗?真是一点头绪都没有!帮助!请和谢谢。

【问题讨论】:

  • 您的 Google Chrome 浏览器版本是多少?
  • 版本 92.0.4515.107 @cruisepandey
  • 可以使用自动下载器,无需手动提供chrome驱动的路径。有关详细信息,请参见下文。
  • 你可以检查一下这个Sessionnotcreated
  • @EaPalacios你能检查系统的杀毒软件或防火墙是否阻止了chromedriver,chrome的执行

标签: python selenium pycharm selenium-chromedriver


【解决方案1】:

使用:-

chromedriver-autoinstaller

即:

自动下载并安装支持的chromedriver 当前安装的chrome版本。此安装程序支持 Linux, MacOS 和 Windows 操作系统。

安装

pip install chromedriver-autoinstaller

用法

只需在要使用的模块中输入import chromedriver_autoinstallerchromedriver

示例

from selenium import webdriver
import chromedriver_autoinstaller


chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                      # and if it doesn't exist, download it automatically,
                                      # then add chromedriver to path

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title

【讨论】:

  • 我的 google chrome 浏览器是 92.0.4515.107,我正在使用自动安装程序,它工作正常。
  • 嗨!我尝试运行代码,但不幸的是弹出了相同的错误消息。
  • 它应该可以工作,因为我有相同的配置,尝试重新启动,看看是否有帮助
【解决方案2】:

您可以使用此解决方案。它会做两件事:

  1. 在您的计算机中本地检查驱动程序版本并进行比较 它与在线提供的最新版本一起使用。
  2. 如果最新的在线版本会自动下载 与您的本地版本不匹配。
from selenium import webdriver
import requests
import zipfile
import wget
import subprocess
import os


CHROMEDRIVER_PATH = "" # Insert your Chromedriver path here
CHROMEDRIVER_FOLDER = os.path.dirname(CHROMEDRIVER_PATH)
LATEST_DRIVER_URL = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"


def download_latest_version(version_number):
    print("Attempting to download latest driver online......")
    download_url = "https://chromedriver.storage.googleapis.com/" + version_number + "/chromedriver_win32.zip"
    # download zip file
    latest_driver_zip = wget.download(download_url, out=CHROMEDRIVER_FOLDER)
    # read & extract the zip file
    with zipfile.ZipFile(latest_driver_zip, 'r') as downloaded_zip:
        # You can chose the folder path to extract to below:
        downloaded_zip.extractall(path=CHROMEDRIVER_FOLDER)
    # delete the zip file downloaded above
    os.remove(latest_driver_zip)


def check_driver():
    # run cmd line to check for existing web-driver version locally
    cmd_run = subprocess.run("chromedriver --version",
                             capture_output=True,
                             text=True)
    # Extract driver version as string from terminal output
    local_driver_version = cmd_run.stdout.split()[1]
    print(f"Local driver version: {local_driver_version}")
    # check for latest chromedriver version online
    response = requests.get(LATEST_DRIVER_URL)
    online_driver_version = response.text
    print(f"Latest online chromedriver version: {online_driver_version}")
    if local_driver_version == online_driver_version:
        return True
    else:
        download_latest_version(online_driver_version)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多