Python中使用Selenium WebDriver报错:'str'对象无'capabilities'属性求解决
解决Selenium初始化时的'str' object has no attribute 'capabilities'错误
这个错误是因为Selenium 4.x及以上版本调整了webdriver.Chrome()的参数规则,不再支持直接传入字符串类型的驱动路径作为第一个参数。以下是两种正确的初始化方式:
方式一:手动指定驱动路径
需要借助selenium.webdriver.chrome.service.Service类封装驱动路径:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 替换为你的Chrome驱动实际本地路径 driver_path = "/your/path/to/chromedriver" service = Service(driver_path) driver = webdriver.Chrome(service=service)
方式二:用ChromeDriverManager自动管理驱动
同样需要结合Service类使用:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager service = Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service=service)
额外注意
如果你的Selenium版本低于4.x,建议升级到最新版本:
pip install --upgrade selenium
内容的提问来源于stack exchange,提问作者Chaitanya Joshi
相关产品推荐
相关产品推荐

