**

反正你就先让selenium点击一下输入框 再用selenium获取值 一般就能解决

**
下面内容ai生成 随便加点内容看看:
做登录自动化或表单采集时,经常会遇到这种现象:
浏览器把账号/密码自动填好了,但用 Selenium 去拿 input 的值(比如 get_attribute(“value”) 或 JS 读 element.value)却是空字符串。很多同学会以为是定位错了、等待不到,其实往往是浏览器的自动填充(autofill)机制在作怪:值并不会立刻同步到 DOM,或者只在用户交互后才真正写回到输入框。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://example.com/login")

wait = WebDriverWait(driver, 10)

# 1) 定位到用户名与密码输入框(以 XPath 为例)
username = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="username"]')))
password = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="password"]')))

# 2) 先点一下触发 autofill 同步
username.click()
password.click()

# 3) 轻微等待,给 autofill 写回 DOM 的时间
driver.implicitly_wait(0.2)  # 或 time.sleep(0.2)

# 4) 再读取 value(get_property 更贴近真实属性)
u = username.get_property("value") or username.get_attribute("value")
p = password.get_property("value") or password.get_attribute("value")

print("username =", u)
print("password =", "*" * len(p))  # 别把明文打到日志里

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐