Python+selenium解决报错:SessionNotCreatedException session not created: No matching capabilities found

问题:
报错:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found

在这里插入图片描述

解决方法
查看导包是否正确:

如果是webdriver.Chrome,那么导包的一切都要是chrome的
如果是webdriver.Firefox,那么导入的包也都源于firefox的包
出现这个问题是因为由于selenium很多类都很相似,如selenium.webdriver.firefox.options与selenium.webdriver.chrome.options,导致导包的时候容易出错,而代码本身是没有问题的。

问题解析
错误代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options #此处导入的是firefox

url = 'https://www.baidu.com'
options = Options()
options.add_argument('--headless')
browser = webdriver.Chrome(options=options) #调用的是Chrome
browser.get(url=url)
print(browser.page_source[:300])
browser.close()
### 关于 Stacktrace 错误的解决方案 在处理 Selenium 自动化测试过程中遇到的 `StaleElementReferenceException` 和 `SessionNotCreatedException` 时,通常会涉及堆栈跟踪 (`Stacktrace`) 的分析。以下是针对这些异常的具体解决方法: #### StaleElementReferenceException 解决方案 当页面上的某个元素被重新加载或者 DOM 发生变化后,原先定位到的元素可能已经失效,这会导致抛出 `selenium.common.exceptions.StaleElementReferenceException: Message:` 异常[^1]。 - **解决策略**: 使用显式等待 (Explicit Waits),确保在操作前再次验证目标元素的存在状态。 ```python from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "element_id")) ) ``` #### SessionNotCreatedException 解决方案 如果出现 `org.openqa.selenium.SessionNotCreatedException: session not created: No matching capabilities found` 异常,则可能是由于浏览器驱动程序与实际使用的浏览器版本不兼容引起的[^2]。 - **解决措施**: - 检查当前安装的 Chrome 浏览器版本号。 ```bash google-chrome --version ``` - 下载对应版本的 Chromedriver 并替换旧版文件。可以访问镜像站点获取所需资源[^3]: [http://npm.taobao.org/mirrors/chromedriver](http://npm.taobao.org/mirrors/chromedriver) #### 堆栈跟踪中的其他常见函数解释 对于提到的一些特定函数名称(如 `GetHandleVerifier`, `simdutf`, 或者线程启动相关),它们通常是操作系统底层调用的一部分,在调试阶段可能会显示出来作为辅助信息帮助开发者追踪问题源头。不过一般情况下无需特别关注这些细节部分除非深入研究具体实现逻辑。 --- ### 提供一段代码示例用于设置正确的 WebDriver 版本匹配流程如下所示: ```python import os from selenium import webdriver chrome_version = 'your_chrome_browser_version_here' chromedriver_path = f'./drivers/{chrome_version}/chromedriver' if not os.path.exists(chromedriver_path): raise FileNotFoundError(f"The chromedriver version {chrome_version} does not exist at path {chromedriver_path}") options = webdriver.ChromeOptions() driver = webdriver.Chrome(executable_path=chromedriver_path, options=options) ``` 通过上述方式能够有效减少因环境配置不当引发的各种运行期错误情况发生几率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值