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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29346595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:26:49  来源:igfitidea点击:

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

pythonseleniumselenium-webdriver

提问by Evg

I am trying to populate form fields via selenium in python:

我正在尝试通过 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")

but this fails with exception:

但这失败了:

Element is not currently interactable and may not be manipulated

元素当前不可交互且不可操作

Why this happens and gow to fix this?

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

webdriver __version__ = '2.45.0'.

网络驱动程序__version__ = '2.45.0'

采纳答案by alecxe

The problem is that there are two other inputelements with placeholder="Логин"and placeholder="Пароль"which are invisible. Make your CSS selectors specific to the login form:

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

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

回答by Andrii

Here xpath selectors working.

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')

or same but css selector

或相同但 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]"]')

回答by Jason J. Nathan

For me, the problem was because another element was overlaying the input box. In my case, there was a fancy label in place of a placeholder that animates upwards on click.

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

The trick was to click on the label first to trigger the animation and then fill in the input field

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

回答by James Green

I had this problem as well, but in my case the cause was slightly different. I needed to specify the size of the window before navigating to the page:

我也有这个问题,但就我而言,原因略有不同。在导航到页面之前,我需要指定窗口的大小:

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

This is because the default window size was rendering the page at a width where a media query had hidden the element, which caused the "Element is not currently interactable and may not be manipulated"error.

这是因为默认窗口大小以媒体查询隐藏元素的宽度呈现页面,这导致“元素当前不可交互且可能无法操作”错误。

回答by Shweta Valunj

Sometimes it takes time to load the page. You can add wait statement. Try using "Thread.sleep(3000)"

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

回答by Kolapo

Use a wait function before the element that gave the error.

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

The webdriver is trying to interact with an element not completely loaded.

webdriver 正在尝试与未完全加载的元素进行交互。