WebElement输入不可交互(从列表中选择特定的输入时)
我正在尝试在此网站上。 我毫无疑问地将其定位在浏览器的控制台中,例如(//输入)[4]
。但是,当我试图在IDE中找到它并在IDE中找到sendkeys(使用Java与Selenium一起使用)时,我得到了 org.openqa.selenium.elementNotNotactableException:element element notable可互作用*例外。我尝试使用以下方法进行操作:
使用简单的发现和sendkeys:
webElement ebookAddinput = driver.findelement(by.xpath(“(// input)[4]”)); ebookaddinput.clear(); ebookaddinput.sendkeys(“ 4”);
使用项目列表选择特定的项目:
list< webElement> allInputs = driver.findelements(by.xpath(“ //输入[@name ='Commerce-add-to-to-cart-Quantity-Input']”)); system.out.println(allinputs.size()); allinputs.get(4).sendkeys(“ 4”);
在两种情况下,我都会收到上述错误。 它工作的唯一方法是不指定我要选择哪个输入,然后没有错误,并且网站上的第一个输入填写:
WebElement ebookAddInput = driver.findElement(By.xpath("//input[@name='commerce-add-to-cart-quantity-input']"));
ebookAddInput.clear();
ebookAddInput.sendKeys("4");
有人知道为什么会发生这种情况以及是否有解决方法? 真的很感谢您的帮助。
I'm trying to find a specific input and sendkeys to it on this website.
I have no problem locating it in the browser's console with for example (//input)[4]
. However, when I'm trying to find it and sendkeys in my IDE (using Java with Selenium) I got the org.openqa.selenium.ElementNotInteractableException: element not interactable* exception. I tried to do it with following methods:
With simple findelement and sendkeys:
WebElement ebookAddInput = driver.findElement(By.xpath("(//input)[4]")); ebookAddInput.clear(); ebookAddInput.sendKeys("4");
Using list of items to choose specific one:
List<WebElement> allInputs = driver.findElements(By.xpath("//input[@name='commerce-add-to-cart-quantity-input']")); System.out.println(allInputs.size()); allInputs.get(4).sendKeys("4");
In both cases I got the mentioned error.
The only way it works is through not specifying which input i want to choose and then there is no error and first input on the website gets filled:
WebElement ebookAddInput = driver.findElement(By.xpath("//input[@name='commerce-add-to-cart-quantity-input']"));
ebookAddInput.clear();
ebookAddInput.sendKeys("4");
Does someone know why is that happening and if theres way to solve it?
Will really appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过以下XPATH
// input [@name ='Commerce-Add-to-Cart-Quantity-Input']
WebDriver可以找到18个元素(请参阅最底部的匹配量,我的照片)。但是,作为用户,当您打开该页面时,您只会在下面的“产品”部分中看到8个产品(在标题中1-1个)。这意味着其他10个元素是隐藏的,而不可交互的。这就是为什么您获得element notintractableException
的原因。因此,您的XPATH必须更具体,因此WebDriver能够找到完全可见/可相互作用的元素。解决方案:
When searching by following xpath
//input[@name='commerce-add-to-cart-quantity-input']
webdriver is able to find 18 elements (see amount of matches at the very bottom on my picture). But as a user, when you open that page, you see only 8 products (1 - in the header, and 7 - in the 'products' section below). Which means that others 10 elements are hidden and not interactable. That's why you getElementNotInteractableException
. So your xpath must be more specific, so that webdriver is able to find exactly visible/interactable elements.Solution: