代码如下:
from selenium import webdriver from selenium.webdriver.common.by import By def test01(): driver = webdriver.Chrome() driver.get("https://www.baidu.com/") driver.find_element(by=By.ID,value='kw').send_keys("你好") driver.find_element(by=By.ID,value='su').click() if __name__ =="__main__": test01()
报错:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 119 Current browser version is 124.0.6367.202 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
参考Selenium官网:Unable to Locate Driver Error,使用logging打印debug日志
代码如下:
import logging from selenium import webdriver from selenium.webdriver.common.by import By logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger('selenium') logger.setLevel(logging.DEBUG) def test01(): driver = webdriver.Chrome() driver.get("https://www.baidu.com/") driver.find_element(by=By.ID,value='kw').send_keys("你好") driver.find_element(by=By.ID,value='su').click() if __name__ =="__main__": test01()
debug日志显示
从日志信息可以看出处理过程:
1、查找Selenium Manager执行文件并调用
2、通过Selenium Manager查找chromedriver 位置和版本
3、通过Selenium Manager查找browser位置和版本
4、判断browser和chromedriver版本是否兼容
DEBUG:selenium.webdriver.common.selenium_manager:Selenium Manager binary found at: H:\codes\python\automation\SeleniumAuto\.venv\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe DEBUG:selenium.webdriver.common.selenium_manager:Executing process: H:\codes\python\automation\SeleniumAuto\.venv\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --debug --language-binding python --output json DEBUG:selenium.webdriver.common.selenium_manager:Found chromedriver 119.0.6045.105 in PATH: H:\study\selenium\drivers\chromedriver.exe DEBUG:selenium.webdriver.common.selenium_manager:chrome detected at C:\Program Files\Google\Chrome\Application\chrome.exe DEBUG:selenium.webdriver.common.selenium_manager:Running command: wmic datafile where name='C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe' get Version /value DEBUG:selenium.webdriver.common.selenium_manager:Output: "\r\r\n\r\r\nVersion=124.0.6367.202\r\r\n\r\r\n\r\r\n\r" DEBUG:selenium.webdriver.common.selenium_manager:Detected browser: chrome 124.0.6367.202 DEBUG:selenium.webdriver.common.selenium_manager:Required driver: chromedriver 124.0.6367.201 WARNING:selenium.webdriver.common.selenium_manager:The chromedriver version (119.0.6045.105) detected in PATH at H:\study\selenium\drivers\chromedriver.exe might not be compatible with the detected chrome version (124.0.6367.202); currently, chromedriver 124.0.6367.201 is recommended for chrome 124.*, so it is advised to delete the driver in PATH and retry DEBUG:selenium.webdriver.common.selenium_manager:Driver path: H:\study\selenium\drivers\chromedriver.exe DEBUG:selenium.webdriver.common.selenium_manager:Browser path: C:\Program Files\Google\Chrome\Application\chrome.exe
删除现有的chromedriver.exe:H:\study\selenium\drivers\chromedriver.exe,让Selenium Manager自己请求合适的chromedriver.exe版本,重新执行程序,程序通过,查看debug日志,详细过程如下:
1、查找Selenium Manager执行文件并调用
2、通过Selenium Manager查找发现没有现成的chromedriver
3、通过Selenium Manager查找browser位置和版本
4、通过Selenium Manager请求兼容brower版本的chromedriver
DEBUG:selenium.webdriver.common.selenium_manager:Selenium Manager binary found at: H:\codes\python\automation\SeleniumAuto\.venv\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe DEBUG:selenium.webdriver.common.selenium_manager:Executing process: H:\codes\python\automation\SeleniumAuto\.venv\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --debug --language-binding python --output json DEBUG:selenium.webdriver.common.selenium_manager:chromedriver not found in PATH DEBUG:selenium.webdriver.common.selenium_manager:chrome detected at C:\Program Files\Google\Chrome\Application\chrome.exe DEBUG:selenium.webdriver.common.selenium_manager:Running command: wmic datafile where name='C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe' get Version /value DEBUG:selenium.webdriver.common.selenium_manager:Output: "\r\r\n\r\r\nVersion=124.0.6367.202\r\r\n\r\r\n\r\r\n\r" DEBUG:selenium.webdriver.common.selenium_manager:Detected browser: chrome 124.0.6367.202 DEBUG:selenium.webdriver.common.selenium_manager:Required driver: chromedriver 124.0.6367.201 DEBUG:selenium.webdriver.common.selenium_manager:Discovering versions from https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json DEBUG:selenium.webdriver.common.selenium_manager:Downloading chromedriver 124.0.6367.201 from https://storage.googleapis.com/chrome-for-testing-public/124.0.6367.201/win64/chromedriver-win64.zip DEBUG:selenium.webdriver.common.selenium_manager:Driver path: C:\Users\lenovo\.cache\selenium\chromedriver\win64\124.0.6367.201\chromedriver.exe DEBUG:selenium.webdriver.common.selenium_manager:Browser path: C:\Program Files\Google\Chrome\Application\chrome.exe DEBUG:selenium.webdriver.common.service:Started executable: `C:\Users\lenovo\.cache\selenium\chromedriver\win64\124.0.6367.201\chromedriver.exe` in a child process with pid: 17216 using 0 to output -3