Home > database >  Webelement input not interactable (when choosing a specific one from list)
Webelement input not interactable (when choosing a specific one from list)

Time:06-22

I'm trying to find a specific input and sendkeys to it on this 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();
        }
    }
}
  • Related