One article to solve: selenium.common.exceptions.SessionNotCreatedException: Message: session not created

1. Cause of the problem

The error message you are encountering indicates that the version of ChromeDriver you are using is incompatible with the version of Google Chrome installed on your computer. ChromeDriver is a standalone executable file that WebDriver uses to control the Chrome browser. To resolve this issue, you have several possible solutions, described below.

2.Solution

1. Modify ChromeDriver version

This method requires you to first check the current Chrome browser version (Open the browser settings and click About Chrome to view), and then go to Corresponding ChromeDriver official websiteChromeDriver driver download addressDownload the driver file consistent with the current Chrome browser, and then query the location of ChromeDriver in the python environment (. Modify the driver version to solve the problem), and finally download the latest version of the driver file and update it to solve the problem. For specific steps, please refer to the blogPress Win+R and enter cmd, then enter where chromedriver
Insert image description here

Method evaluation: I think this method is not very good, because the Chrome browser will automatically update all the time, and you always need to manually update the ChromeDriver version and replace it according to the Chrome version update. , it cannot be done once and for all, and I do not recommend this method here.

2. Downgrade Google Chrome

If your ChromeD cannot be updated to the corresponding version of ChromeDriver because the version is too high, you can try to downgrade the Chrome browser version to a version that matches the version currently supported by ChromeDriver. So just uninstall the current version of Chrome, then download and install the older version corresponding to ChromeDriver.
Method evaluation: This method is still not recommended, because uninstalling and installing Chrome itself is more cumbersome and time-consuming. After downloading, Chrome often defaults to the latest version. , and if you download and install an old version, the browser will automatically update to the latest Chrome version. If you want to prevent updates, you need to turn off Chrome's automatic updates. In short, it is very tedious and complicated to implement, so I don't recommend it.

3.Use WebDriverManager

WebDriverManager is a librarythat automatically downloads the appropriate WebDriver executable file based on the browser version you are using. It eliminates the need to manually manage WebDriver binaries. You can install WebDriverManager using pip (Python package manager) and then modify your code to use WebDriverManager to handle the WebDriver binary.
The specific implementation steps are as follows:
1. Download and install the WebDriverManager module;

pip install webdriver_manager

2. Modify the code to use WebDriverManager to manage the driver

#配置参数
from selenium.webdriver import Chrome
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disbale-gpu")
web = Chrome(options=opt)

# 将上述代码修改为===>
from selenium.webdriver import Chrome
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
# 创建一个Service对象
service = Service(ChromeDriverManager().install())
#配置参数
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disbale-gpu")
web = Chrome(service=service, options=opt)
# 即可解决问题

Guess you like

Origin blog.csdn.net/qq_51447436/article/details/133612604