Selenium报错AttributeError: 'str'对象无'start'属性求助
问题解决:AttributeError: 'str' object has no attribute 'start'
错误原因
- Selenium 4+ API变更:
webdriver.Chrome()的service参数需要接收Service类实例,而非字符串路径。直接传入字符串会导致代码尝试调用字符串的start()方法,触发属性错误。 - 浏览器路径配置错误:
chromeOptions.binary_location应该指向Chrome浏览器的二进制文件,而非chromedriver驱动程序的路径。
修正后的代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 导入Service类 from selenium.webdriver.common.keys import Keys chromeOptions = webdriver.ChromeOptions() # 替换为你环境中Chrome浏览器的实际路径,示例为Linux常见路径 chromeOptions.binary_location = "/usr/bin/google-chrome" chromeDriver_path = 'VirtualBrowser/chromedriver' # 用Service类包装驱动路径后传入service参数 driver = webdriver.Chrome(service=Service(chromeDriver_path), options=chromeOptions) driver.get("https://google.com") input("Running...")
额外说明
- 查找Chrome浏览器路径的方法:
- Linux:终端执行
which google-chrome - macOS:终端执行
which google-chrome,或直接使用路径/Applications/Google Chrome.app/Contents/MacOS/Google Chrome - Windows:默认路径为
C:\Program Files\Google\Chrome\Application\chrome.exe
- Linux:终端执行
- 必须保证chromedriver版本与Chrome浏览器版本完全匹配,版本不兼容会引发其他异常。
内容的提问来源于stack exchange,提问作者szal
相关产品推荐
相关产品推荐

