EasyJob Meistars
EasyJob Meistars

Reputation: 23

Continue if element not found, if found save it

I have a loop, where I open links one by one. Inside this loop I have the if statement, which checks:

  1. If I see name, then I copy it
  2. If I don't see name, then I ignore it and continue looping.

    List<WebElement> demovar = driver.findElements(By.xpath("//*[@id=\"big_icon_view\"]/ul/li/p/a"));
    System.out.println(demovar.size());
    ArrayList<String> hrefs = new ArrayList<String>(); 
    for (WebElement var : demovar) {
        System.out.println(var.getText());
        System.out.println(var.getAttribute("href"));
        hrefs.add(var.getAttribute("href"));
    }
    
    int i = 0;
    for (String href : hrefs) {
        driver.navigate().to(href);
        System.out.println((++i) + ": navigated to URL with href: " + href);
        if(driver.findElement(By.xpath("//a[@id='name']")).isDisplayed()) {
            System.out.println("I can see Name");
        } else {
            System.out.println("I cant see Name");
        }
        Thread.sleep(3000); // To check if the navigation is happening properly.
    }
    

Why is this not working properly? As I assume, it should have the following:

  1. If the element is displayed then I can see Name
  2. else the element is NOT displayed, then I cannot see Name.

Upvotes: 0

Views: 1304

Answers (3)

EasyJob Meistars
EasyJob Meistars

Reputation: 23

After @Christine help, i did a solution for me

for (String href : hrefs) {
            driver.navigate().to(href);
            boolean isPresent = driver.findElements(By.xpath("element")).size() > 0;
            if (isPresent) {
                String test = driver.findElement(By.xpath("element")).getText();
                System.out.println(test);
            } else {
                System.out.println("Name not found");
            }
            Thread.sleep(3000); // To check if the navigation is happening properly.
        }
    }
}

And this is working fine =)

Upvotes: 0

Venkatesh Manohar
Venkatesh Manohar

Reputation: 2125

API of the findElement(By by) in the Interface WebDriver states the following

"Find the first WebElement using the given method. This method is affected by the 'implicit wait' times in force at the time of execution. The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached. findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead."

Which means that in case if the element is not found it keeps trying until configured timeout and throws the exception NoSuchElementException - If no matching elements are found

Hence it would be better to handle in the following ways

  1. Using FindElements which returns a list of all WebElements, or an empty list if nothing matches as follows:

    if(driver.findElements(By.ByXPath).size()<0)

  2. Using a try/catch/finally to catch the NoSuchElementException and a boolean flag to determine if its present or not. The boolean flag can be set to false in case if exception is caught.

Upvotes: 0

CEH
CEH

Reputation: 5909

I'm not sure what error message you are seeing here, but if your code is not working then it's quite likely the element is not displayed on the page, so you will receive an exception when attempting to locate it.

You can catch the NoSuchElementException to handle the case where the element does not appear on the page.

 for (String href : hrefs) {
    driver.navigate().to(href);
    System.out.println((++i) + ": navigated to URL with href: " + href);
    // create isDisplayed variable
    boolean isDisplayed = true;
    try {
        isDisplayed = driver.findElement(By.xpath("//a[@id='name']")).isDisplayed();
        }
    catch(NoSuchElementException) {
            isDisplayed = false;
        }
        // do something else here with isDisplayed
        if (isDisplayed) { System.out.println("I can see Name"); }
        else { System.out.println("I can not see Name"); }
}

This code does almost the same thing as yours, but we catch the NoSuchElementException that gets thrown if the element does not appear on the page.

If this does not work for you, feel free to post the error message or results you are seeing in your code, it'll help track down the issue.

Upvotes: 1

Related Questions