You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium报错AttributeError: 'str'对象无'start'属性求助

问题解决:AttributeError: 'str' object has no attribute 'start'

错误原因

  1. Selenium 4+ API变更:webdriver.Chrome()的service参数需要接收Service类实例,而非字符串路径。直接传入字符串会导致代码尝试调用字符串的start()方法,触发属性错误。
  2. 浏览器路径配置错误: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
  • 必须保证chromedriver版本与Chrome浏览器版本完全匹配,版本不兼容会引发其他异常。

内容的提问来源于stack exchange,提问作者szal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:25:21