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

浏览器控制台与Selenium查询返回值不一致问题求助

解决方法

方法1:给JavaScript脚本添加return语句

你当前的execute_script调用仅访问了value属性,但没有主动返回它。只需在脚本里加上return关键字,就能获取到目标值:

self.driver.execute_script("return document.getElementsByName('url')[0].value")

方法2:使用Selenium原生API获取值(更推荐)

无需借助JavaScript,直接用Selenium的元素定位方法找到输入框,再调用get_attribute('value')即可读取当前值:

from selenium.webdriver.common.by import By

url_input = self.driver.find_element(By.NAME, "url")
current_value = url_input.get_attribute('value')

补充说明

即便用户通过浏览器控制台这类外部方式修改了输入框内容,Selenium的get_attribute('value')依然能正确读取到最新值,因为它直接访问元素的当前DOM属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:34:53