Python Selenium单测正常,defaultTestLoader执行报ChromeDriver版本错误
问题:Selenium测试通过unittest.defaultTestLoader运行时出现ChromeDriver版本不匹配错误
单独运行Selenium测试文件时可正常执行,但使用unittest.defaultTestLoader加载测试时抛出如下错误:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 90 Current browser version is 115.0.5790.102 with binary path C:/Program Files/Google/Chrome/Application/chrome.exe
已将Selenium更新至最新版本,在PyCharm中直接点击运行测试文件时,Chrome可正常启动且测试通过,但通过测试加载器运行(单测或多测)均失败。该问题是近期因ChromeDriver位置变更修改代码并更新Selenium后出现的。
相关代码片段
测试加载器代码
test_suite = unittest.defaultTestLoader.discover(start_dir='./tests', pattern='ExampleTest.py') combo_suite = unittest.TestSuite(test_suite) runner.run(combo_suite)
Chrome启动代码
@classmethod def setUpClass(cls): chrome_options = Options() chrome_options.add_argument("--remote-debugging-port=9222") chrome_options.binary_location= "C:/Program Files/Google/Chrome/Application/chrome.exe" cls.driver = webdriver.Chrome(options=chrome_options) cls.driver.maximize_window()
临时可行方案
手动下载对应Chrome版本的ChromeDriver文件,在代码中指定其路径后,两种运行方式均正常执行:
service = Service(executable_path="/drivers/chromedriver.exe") cls.driver = webdriver.Chrome(service=service, options=chrome_options)
内容的提问来源于stack exchange,提问作者dzheng
相关产品推荐
相关产品推荐

