Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can not click on an element that is inside a ComboBox

I got the contents of a list (ul) using xpath and put it in a variable of type List . Then I looped the variable elements and had them print the contents of the list.

The eclipse displayed the list with all the options contained in the list (li). Inside the loop I made an IF to look for the GEOCF option and, if it is found, the script will click. Even the GEOCF option exists, I can not click on it and the routine goes to the ELSE.

It follows the source code and the print of the images.

public void selecionarAuditoriaNacional() throws Exception {

    WebElement findElement = driver.findElement(By.id("consultarUnidadeAuditoria"));
    findElement.click();

    List<WebElement> elementos = driver.findElements(By.xpath("//div[@class = 'ui-selectonemenu-items-wrapper']/ul"));

    for (WebElement elementosGrid : elementos) {

        System.out.println(elementosGrid.getText());

        if (elementosGrid.equals("GEOCF")) {

            elementosGrid.click();

        } else {

            System.out.println("Erro");

        }

    }

}

ComboBox

Console Eclipse

like image 557
Kakashi - Sensei Avatar asked Dec 31 '25 22:12

Kakashi - Sensei


1 Answers

The GEOCF is a list element and reside in the li tag. But you are getting all ul tag in your elements list.

Change the xpath locator to find li and one typo in if condition changed elementosGrid.equals("GEOCF") to elementosGrid.getText().equals("GEOCF")

List<WebElement> elementos = driver.findElements(By.xpath("//div[@class = 'ui-selectonemenu-items-wrapper']/ul/li"));

for (WebElement elementosGrid : elementos) {

    System.out.println(elementosGrid.getText());

    if (elementosGrid.getText().equals("GEOCF")) {

        elementosGrid.click();

    } else {

        System.out.println("Erro");

    }

}

Or

you can remove the entire loop & if and handle find element with text with single xpath

public void selecionarAuditoriaNacional() throws Exception {

  WebElement findElement = driver.findElement(By.id("consultarUnidadeAuditoria"));
  findElement.click();

  WebDriverWait wait = new WebDriverWait (driver, 60);
  elementosGrid=wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath("//div[@class = 'ui-selectonemenu-items-wrapper']/ul/li[text() = 'GEOFC']")));
  elementosGrid.click()
}
like image 143
Navarasu Avatar answered Jan 04 '26 15:01

Navarasu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!