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

Python Selenium无法选择下拉框值,报TypeError: 'str' object is not callable错误

问题解决:Selenium中TypeError: 'str' object is not callable

错误原因

你写的By.XPATH("//select/option[@value='3']")是错误语法——By.XPATH是字符串类型的常量,不是函数,不能直接传参调用。find_element方法需要传入两个参数:定位方式和对应的定位表达式。

修复方案

方式1:修正基础定位写法

from selenium.webdriver.common.by import By

# 分开传递定位方式与表达式
your_choice = browser.find_element(By.XPATH, "//select/option[@value='3']")
your_choice.click()

方式2:用Select类操作下拉框(更稳定)

如果目标选项属于<select>标签,Selenium提供的Select类专门用于处理下拉选择,比直接点击option更可靠:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

# 先定位到select标签
select_element = browser.find_element(By.XPATH, "//select")
# 初始化Select对象
select = Select(select_element)
# 通过value值选中目标选项
select.select_by_value('3')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 15:06:01