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

Selenium与Python报错:'str'对象无对应属性问题求助

问题解决:'str' object has no attribute '_ignore_local_proxy'

错误根源

你遇到的错误核心是Selenium 4.x版本的API参数变化:旧版本中webdriver.Chrome()可以直接传入驱动路径,但4.x版本后,驱动路径需要通过Service类传递,直接传字符串会被程序误判为Options对象,从而触发属性查找错误。另外你的代码还有缩进、重复配置、路径适配(Kali是Linux系统,不能用Windows的C:\路径)、缺失导入等问题。

修正步骤及代码

  1. 用Service类正确传递Chromedriver路径(适配Kali Linux环境,chromedriver一般安装在/usr/bin/chromedriver,如果是自行下载的请替换为实际路径)
  2. 合并重复的ChromeOptions配置,避免冗余
  3. 修复代码缩进错误
  4. 导入缺失的WebDriverWait

修正后的完整代码:

import sys
import datetime
import selenium
import requests
import time as t

from sys import stdout
from selenium import webdriver
from optparse import OptionParser
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait  # 补全缺失的导入

# 统一配置ChromeOptions
chrome_options = Options()
chrome_options.add_argument('--proxy-server=http://ipaddress:port')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.ignore_local_proxy_environment_variables()
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--disable-extensions")

# Config
parser = OptionParser()
now = datetime.datetime.now()

# Kali Linux下的chromedriver路径,自行根据实际情况调整
CHROME_DVR_DIR = '/usr/bin/chromedriver'

# 初始化浏览器实例
service = Service(CHROME_DVR_DIR)
browser = webdriver.Chrome(service=service, options=chrome_options)
wait = WebDriverWait(browser, 10)
count = 1  # 计数变量

关键说明

  • Kali Linux是类Unix系统,路径使用斜杠/而非Windows的反斜杠\,如果是手动下载的chromedriver,要确保路径正确且有执行权限(可通过chmod +x /path/to/chromedriver添加权限)
  • Selenium 4.x中,webdriver.Chrome()的第一个参数不再是驱动路径,必须通过service参数传递驱动服务实例,options参数传递配置项
  • 避免重复创建ChromeOptions和webdriver.Chrome实例,减少资源消耗和冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 12:52:29