Search code examples
javaseleniumselenium-webdriverselenium-java

How to check if an element is visible with WebDriver - eternal loading


I'm testing a button that close a window. Then when I clicking in the button I need check If the Window closed.

I created this method:

enter code here




public boolean isElementPresent(String locatorKey) {
    try {
        driver.findElement(By.xpath(locatorKey));
        return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
        Assert.assertFalse((isElementPresent(String.valueOf(By.xpath(locatorKey)))));
        return false;
    }
}





enter code here

But it`s not working. Selenium stays loading the last step for a long time and in the end shows this error

Command: [8139ce6f8c35642a752d7fbedb186ec6, findElement {using=xpath, value=By.xpath: By.xpath: By.xpath: By.xpath: By.xpath: By.xpath: By.xpath: By.xpath: By.xpath: By.xpath:


Solution

  • public void isElementPresent(By Xpath) {
        
        boolean isPresente;
    
        try {
            driver.findElement(By.xpath("//div[contains(@data-component-name,'cardboard')]"));
            driver.findElement(Xpath);
            System.out.println("Elemento está na tela");
    
            isPresente = true;
    
        } catch (NoSuchElementException e) {
            
            System.out.println("Elemento não está na tela");
            isPresente = false;
        }
    
        Assert.assertFalse(isPresente);
    }