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

使用Python Selenium获取input元素值返回None的问题求助

问题排查与解决方案

问题原因

  • 目标input元素的有效值存储在value属性中,但你的代码错误地尝试获取不存在的href属性,因此返回None。
  • 目标元素的id是唯一标识(a0),使用find_elements返回列表再循环属于冗余操作,直接用find_element获取单个元素更高效。

解决方案

将获取属性的参数改为value,同时优化元素定位方式:

修正后的代码(推荐)

item = driver.find_element(By.XPATH, "//input[@id='a0']")
input_value = item.get_attribute('value')
print(input_value)

若需保留列表遍历(不推荐)

items = driver.find_elements(By.XPATH, "//input[@id='a0']")
for item in items:
    input_value = item.get_attribute('value')
    print(input_value)

执行以上代码后,即可输出期望的scarping-test。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 22:45:13