使用Python+Selenium调用Chrome时出现Attribute Error问题求助
解决Selenium调用Chrome时的AttributeError: 'str' object has no attribute 'capabilities'
这个错误是因为Selenium 4.x及以上版本调整了API,webdriver.Chrome() 不再直接接受驱动文件路径字符串,必须通过 Service 类来传递驱动路径。
修正后的代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager # 用Service类封装ChromeDriver路径 service = Service(ChromeDriverManager().install()) browser = webdriver.Chrome(service=service) browser.get('https://google.com')
手动指定驱动文件的情况
如果是手动下载了ChromeDriver,也需要用Service封装路径:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 替换为你的ChromeDriver实际路径 service = Service('/path/to/your/chromedriver') browser = webdriver.Chrome(service=service) browser.get('https://google.com')
内容的提问来源于stack exchange,提问作者abd hadi Sakbani
相关产品推荐
相关产品推荐

