Chromedriver in Python error SessionNotCreatedException



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

文件路径为">C:Usersea.palaciosDownloadschromedriver.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:Usersea.palaciosDownloadschromedriver.exe')

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

Traceback (most recent call last):
File "C:Usersmarcd.adminPycharmProjectsPLDTmain.py", line 3, in <module>
driver = webdriver.Chrome(executable_path='C:\Users\ea.palacios\Downloads\chromedriver.exe')
File "C:Usersmarcd.adminPycharmProjectsPLDTvenvlibsite-packagesseleniumwebdriverchromewebdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:Usersmarcd.adminPycharmProjectsPLDTvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:Usersmarcd.adminPycharmProjectsPLDTvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:Usersmarcd.adminPycharmProjectsPLDTvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:Usersmarcd.adminPycharmProjectsPLDTvenvlibsite-packagesseleniumwebdriverremoteerrorhandler.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)

下面是代码和错误信息的截图

这可能是因为我在我的工作计算机上运行PyCharm作为管理员,但Chromedriver保存在我的常规下载文件夹中?真的不知道!的帮助!请和谢谢。

使用说明:-

chromedriver-autoinstaller h1> :

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

安装

pip install chromedriver-autoinstaller

使用

在要使用chromedriver的模块中输入import chromedriver_autoinstaller

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

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

  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)

相关内容

  • 没有找到相关文章

最新更新