python selenium - 元素当前不可交互且可能无法操作

python selenium - Element is not currently interactable and may not be manipulated

我正在尝试通过 python 中的 selenium 填充表单字段:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()        
driver.get("http://www.miralinks.ru/")
driver.implicitly_wait(30)
login = driver.find_element_by_css_selector('input[placeholder="Логин"]')
hov = ActionChains(driver).move_to_element(login)
hov.perform()
login.clear()
login.send_keys("login")
pwd = driver.find_element_by_css_selector('input[placeholder="Пароль"]')
pwd.clear()
pwd.send_keys("pass")

但这失败了,异常:

Element is not currently interactable and may not be manipulated

为什么会发生这种情况并解决这个问题?

webdriver __version__ = '2.45.0'.

问题是还有另外两个 input 元素 placeholder="Логин"placeholder="Пароль" 是不可见的。使您的 CSS 选择器特定于登录表单:

login = driver.find_element_by_css_selector('form#loginForm input[placeholder="Логин"]')
pwd = driver.find_element_by_css_selector('form#loginForm input[placeholder="Пароль"')

此处 xpath 选择器有效。

login = driver.find_element_by_xpath('//*[@id=\'loginForm\']/div/div[1]/input')
pwd = driver.find_element_by_xpath('//*[@id=\'loginForm\']/div/div[2]/input')

或相同但 css 选择器

login = driver.find_element_by_css_selector('form#loginForm [name="data[User][login]"]')
pwd = driver.find_element_by_css_selector('form#loginForm [name="data[User][password]"]')

对我来说,问题是因为另一个元素覆盖了输入框。在我的例子中,有一个花哨的标签代替了一个在点击时向上动画的占位符。

诀窍是先点击标签触发动画,然后填写输入字段

我也遇到了这个问题,但在我的情况下,原因略有不同。在导航到页面之前,我需要指定 window 的大小:

driver.Manage().Window.Size = new Size(width, height);

这是因为默认 window 大小以媒体查询隐藏元素的宽度呈现页面,这导致 "Element is not currently interactable and may not be manipulated"错误。

有时加载页面需要一些时间。您可以添加等待语句。 尝试使用 "Thread.sleep(3000)"

在出现错误的元素之前使用等待函数。

网络驱动程序正在尝试与未完全加载的元素交互。