2

first time poster, long time user reaping the benefits of all these great questions. But I need your help.

What I'm trying to do below is

  1. Navigate to a page
  2. Find all the particular links
  3. Click on the first link
  4. Check to see if an element is displayed, if it is displayed then navigate back to the previous page and click on the next link of the list. If it is NOT displayed then exit out of the method and continue the test script. This is the part where I'm stuck.

The if statement executes as desired whereby if it finds the element then it navigates back to the previous. But where it fails is when it clicks on the second link of the page. It searches for that element even though that element does not exist in that page and does not exit out of the method even though I've explicitly stated return.

I'm having a brain fart and tried all the possible combinations and permuatations I can think of. If there's anyone out there that can help me I'd greatly appreciate the help.

EDIT

Let me edit to clarify my thoughts. I need my method to exit out of the method once inactive.isDisplayed() returns false. But when it navigates to the second page, it continually tries to find the element then eventually fails with a NoSuchElementException. I know the element doesn't exist, that's why I need it to exit out of the method and perform the next step of the test script. I hope this clarify my situation. It's not really a Selenium WebDriver question as it is a java question.

Thanks

public void checkErrors() {
    List<WebElement> videos =driver.findElements(By.cssSelector(".row-
    title"));
    for (int i = 0; i < videos.size(); i++) {
        videos = driver.findElements(By.cssSelector(".row-title"));
        videos.get(i).click();
        if (inactive().isDisplayed() != false) {
            driver.navigate().back();
        } else {
            return;
        }
    }
    return;
}

EDIT:

private WebElement inactive() {
    inactive = 
    driver.findElement(By.cssSelector("#message>p>strong"));
    highlightElement(inactive);
    return inactive;
}
7
  • Hi Sammy, could you share the code for the inactive() method? Commented Mar 24, 2016 at 12:59
  • Hi Tom, I've edited my post with the inactive method. Although it is working perfectly fine. It's basically my java logic that needs some correcting. Commented Mar 24, 2016 at 13:05
  • By not exit you mean the for keeps looping or you have an error? Commented Mar 24, 2016 at 13:10
  • Hi Guy, I've edited my post to clarify what I'm after so I'll post it here as well: Let me edit to clarify my thoughts. I need my method to exit out of the method once inactive.isDisplayed() returns false. But when it navigates to the second page, it continually tries to find the element then eventually fails with a NoSuchElementException. I know the element doesn't exist, that's why I need it to exit out of the method and perform the next step of the test script. I hope this clarify my situation. It's not really a Selenium WebDriver question as it is a java question. Thanks Commented Mar 24, 2016 at 13:12
  • Have you tried catching the NoSuchElement exception in the inactive method? Commented Mar 24, 2016 at 13:16

2 Answers 2

1

You might want to check the presence of the message before checking if it's displayed:

public void checkErrors() {
    for(int i = 0; ; i++) {
        // handle next link
        List<WebElement> videos = driver.findElements(By.cssSelector(".row-title"));
        if (i >= videos.size())
            return;

        // click the next link
        WebElement video = videos.get(i);
        video.click();

        // return if the message is missing or hidden
        List<WebElement> messages = driver.findElements(By.cssSelector("#message>p>strong"));
        if (messages.size() == 0 || !messages.get(0).isDisplayed())
            return;

        driver.navigate().back();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Florent, this worked! Thanks so much for your help! I greatly appreciate it :)
0

Small recommendation to help you here:

As you're not using the WebElement returned by inactive() after you've checked if it's displayed or not, you might as well move the logic for checking whether its displayed to inactive() and return the value of isDisplayed(). For example:

private boolean inactive() {
   try {
      inactive = driver.findElement(By.cssSelector("#message>p>strong"));
      highlightElement(inactive);
      return inactive.isDisplayed(); // Element is present in the DOM but may not visible due to CSS etc.
   } catch (NoSuchElementException e) {
      return false; // Element is not present in the DOM, therefore can't be visible.
   }
}

2 Comments

Hi Tom, your solution worked as well! Although I can't make videos null otherwise I won't be able to click on the video. I know I wasn't clear enough in my question but thanks for the help, I greatly appreciate it.
Doh forgot size of the video list was used in the for loop!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.