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

Selenium使用Xpath定位元素报错'str' object is not callable如何解决?

问题根因

你触发报错的核心原因是find_element方法传参格式不符合新版Selenium规范:

  • By.XPATH是Selenium预定义的字符串常量,本身不是可调用的函数,你写的By.XPATH("//*[@id='sign']")属于错误地将字符串作为方法调用,直接触发TypeError: 'str' object is not callable报错。
  • 你代码中使用的chrome_options参数在新版Selenium中已被弃用,运行时会触发弃用警告。
修正后可运行代码
from selenium.webdriver.common.by import By
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('ignore-certificate-errors')

# 弃用的chrome_options参数替换为options,适配新版本Selenium
driver = webdriver.Chrome(options=options)
driver.get('cannot_share_this_link/index.php')

# 正确传参格式:第一个参数为定位方式常量,第二个为定位表达式
driver.find_element(By.XPATH, "//*[@id='sign']")
可选优化

你需要定位的元素本身带有明确的id属性,直接使用By.ID定位效率更高,写法如下:

driver.find_element(By.ID, "sign")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 16:54:02