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

如何使用Selenium移除HTML元素中的autocomplete='off'属性?

用Selenium移除HTML元素的autocomplete="off"属性

针对你提供的id为searchInput的input元素,以下是两种可行的实现方式:

方法1:直接移除autocomplete属性

通过执行JavaScript代码,调用DOM元素的removeAttribute方法删除目标属性:

Python 示例

driver.execute_script("document.getElementById('searchInput').removeAttribute('autocomplete');")

Java 示例

((JavascriptExecutor) driver).executeScript("document.getElementById('searchInput').removeAttribute('autocomplete');");

方法2:修改属性值为"on"

部分场景下,直接修改属性值为浏览器默认允许自动填充的on,效果比移除属性更稳定:

Python 示例

driver.execute_script("document.getElementById('searchInput').setAttribute('autocomplete', 'on');")

Java 示例

((JavascriptExecutor) driver).executeScript("document.getElementById('searchInput').setAttribute('autocomplete', 'on');");

说明:Selenium核心API没有直接操作DOM属性的方法,因此需要借助JavaScript来直接操作页面元素的属性,这是处理这类DOM属性修改需求的通用方案。

内容的提问来源于stack exchange,提问作者王云湖不归

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:05:32