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

Selenium Python 3.7 Chrome驱动报错求助:文献导出与单选框切换

问题分析与解决方案

核心错误点梳理

  1. 参数拼写错误:初始代码中execeutable_path是拼写错误,正确应为executable_path;且Selenium 4.x版本后推荐用Service类管理驱动,而非直接传路径参数。
  2. Chrome配置混淆:binary_location是用来指定Chrome浏览器程序的路径(比如C:\Program Files\Google\Chrome\Application\chrome.exe),而非ChromeDriver驱动的路径,你搞反了两者的作用。
  3. 配置被覆盖:代码中先创建options = webdriver.ChromeOptions(),随后又赋值options = Options(),导致之前的配置全部丢失。
  4. 元素定位错误:ng-valid ng-dirty ng-touched是元素的class属性值,不是id,用find_element_by_id必然定位失败;且后续判断选中状态时,错误调用webdriver.find_element_by_id,应该用已创建的driver实例。

修复后的完整代码

推荐使用webdriver-manager自动管理ChromeDriver,彻底解决路径和版本匹配问题,同时修正元素定位逻辑:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

# 配置Chrome选项
options = Options()
# 如果需要无头模式,取消下面注释
# options.add_argument("--headless")
# 可选:指定Chrome浏览器路径(如果系统默认路径找不到Chrome时才需要)
# options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

# 自动下载并匹配对应版本的ChromeDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.maximize_window()

# 目标页面
driver.get("https://ieeexplore.ieee.org/xpl/conhome/9811522/proceeding")

# --- 修正元素定位:以第一个文献的复选框为例 ---
# 先找到第一个文献的复选框(实际需根据页面真实元素调整,这里用class示例,建议用开发者工具复制正确的选择器)
first_checkbox = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox'].ng-valid.ng-dirty.ng-touched")
first_checkbox.click()

# 验证选中状态
status = first_checkbox.is_selected()
print(f"复选框是否选中:{status}")

# --- 后续导出步骤示例 ---
# 点击导出按钮(需替换为真实元素选择器)
export_btn = driver.find_element(By.CSS_SELECTOR, "button[aria-label='Export']")
export_btn.click()

# 等待弹窗加载(建议用显式等待,避免硬编码sleep)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待弹窗出现,选择"引用和摘要"选项(示例选择器,需根据实际页面调整)
wait = WebDriverWait(driver, 10)
citation_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='引用和摘要']")))
citation_option.click()

# 点击导出按钮(示例选择器)
final_export_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click='export()']")))
final_export_btn.click()

# 等待下载完成后关闭浏览器
# driver.quit()

关键注意事项

  1. 安装依赖:先执行pip install selenium webdriver-manager安装所需包。
  2. 元素选择器:页面元素可能会更新,务必用Chrome开发者工具(F12)重新获取最新的元素选择器,不要直接用代码中的示例选择器。
  3. 显式等待:避免使用time.sleep(),改用WebDriverWait等待元素加载,提升脚本稳定性。
  4. Chrome版本匹配:webdriver-manager会自动下载与本地Chrome版本一致的驱动,无需手动下载和指定路径,彻底解决路径和版本不兼容问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:08:08