WebElement输入不可交互(从列表中选择特定的输入时)

发布于 2025-02-09 11:22:45 字数 1091 浏览 1 评论 0原文

我正在尝试在此网站上。 我毫无疑问地将其定位在浏览器的控制台中,例如(//输入)[4]。但是,当我试图在IDE中找到它并在IDE中找到sendkeys(使用Java与Selenium一起使用)时,我得到了 org.openqa.selenium.elementNotNotactableException:element element notable可互作用*例外。我尝试使用以下方法进行操作:

  1. 使用简单的发现和sendkeys:

      webElement ebookAddinput = driver.findelement(by.xpath(“(// input)[4]”));
    ebookaddinput.clear();
    ebookaddinput.sendkeys(“ 4”);
     
  2. 使用项目列表选择特定的项目:

      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:

  1. With simple findelement and sendkeys:

    WebElement ebookAddInput = driver.findElement(By.xpath("(//input)[4]"));
    ebookAddInput.clear();
    ebookAddInput.sendKeys("4");
    
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-02-16 11:22:45

通过以下XPATH // input [@name ='Commerce-Add-to-Cart-Quantity-Input'] WebDriver可以找到18个元素(请参阅最底部的匹配量,我的照片)。但是,作为用户,当您打开该页面时,您只会在下面的“产品”部分中看到8个产品(在标题中1-1个)。这意味着其他10个元素是隐藏的,而不可交互的。这就是为什么您获得element notintractableException的原因。因此,您的XPATH必须更具体,因此WebDriver能够找到完全可见/可相互作用的元素。

解决方案:

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.util.List;

public class SeleniumNotInteractable {
    WebDriver driver;

    @Test
    public void test() throws Exception {
        // download driver if not exist (for more details google 'how to use bonigarcia selenium')
        WebDriverManager.chromedriver().setup();

        driver = new ChromeDriver();
        driver.get("https://www.trychomaster.com/sklep");

        // wait until section with all products appear at the bottom of the page. You can wait for some specific products
        // to be visible/clickable, or, like I am showing below, you can wait for the whole section to be visible:
        WebElement contentSection = driver.findElement(By.cssSelector("div.content-section"));
        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.attributeContains(contentSection, "style", "opacity: 1"));
        System.out.println("Content section of the page become visible!");

        // fill price for that single product at the top of the page
        String headerPriceInputXpath = "//*[@class='heading-30']//following::div[1]//input[@name='commerce-add-to-cart-quantity-input']";
        WebElement headerProductInput = driver.findElement(By.xpath(headerPriceInputXpath));
        headerProductInput.clear();
        headerProductInput.sendKeys("5");

        // fill prices for all other products at the bottom of the page
        String allOtherProductsInputsXpath = "//div[@data-w-tab='Produktys']//div[@role='listitem']//input[@name='commerce-add-to-cart-quantity-input']";
        List<WebElement> allOtherProductInputs = driver.findElements(By.xpath(allOtherProductsInputsXpath));
        for (WebElement productInput : allOtherProductInputs) {
            productInput.clear();
            productInput.sendKeys("5");
        }

        Thread.sleep(10 * 1000); // sleep 10 secs, to prevent browser closing, can be removed
    }

    @AfterTest
    public void tearDown() {
        // quit
        if (driver != null) {
            driver.quit();
        }
    }
}

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 get ElementNotInteractableException. So your xpath must be more specific, so that webdriver is able to find exactly visible/interactable elements.

enter image description here

Solution:

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.util.List;

public class SeleniumNotInteractable {
    WebDriver driver;

    @Test
    public void test() throws Exception {
        // download driver if not exist (for more details google 'how to use bonigarcia selenium')
        WebDriverManager.chromedriver().setup();

        driver = new ChromeDriver();
        driver.get("https://www.trychomaster.com/sklep");

        // wait until section with all products appear at the bottom of the page. You can wait for some specific products
        // to be visible/clickable, or, like I am showing below, you can wait for the whole section to be visible:
        WebElement contentSection = driver.findElement(By.cssSelector("div.content-section"));
        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.attributeContains(contentSection, "style", "opacity: 1"));
        System.out.println("Content section of the page become visible!");

        // fill price for that single product at the top of the page
        String headerPriceInputXpath = "//*[@class='heading-30']//following::div[1]//input[@name='commerce-add-to-cart-quantity-input']";
        WebElement headerProductInput = driver.findElement(By.xpath(headerPriceInputXpath));
        headerProductInput.clear();
        headerProductInput.sendKeys("5");

        // fill prices for all other products at the bottom of the page
        String allOtherProductsInputsXpath = "//div[@data-w-tab='Produktys']//div[@role='listitem']//input[@name='commerce-add-to-cart-quantity-input']";
        List<WebElement> allOtherProductInputs = driver.findElements(By.xpath(allOtherProductsInputsXpath));
        for (WebElement productInput : allOtherProductInputs) {
            productInput.clear();
            productInput.sendKeys("5");
        }

        Thread.sleep(10 * 1000); // sleep 10 secs, to prevent browser closing, can be removed
    }

    @AfterTest
    public void tearDown() {
        // quit
        if (driver != null) {
            driver.quit();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文