0

I have a page with a list of 10 different elements links , I clicao an element , go to page specifies it, realize the operations that need to perform and then return to the list of elements .

After performing the operations , I need to return to the list of items and click the next element and not on the same element that clicked previously .

How do I click the next element of the list and not in LEMENT already clicked?

Obs .: I do not have access to the source code.

basic structure of the page:

<!DOCTYPE html>
<html>
<body>

<h2>Page Test</h2>

    <div id="results-container"><ol id="results" class="search-results">

    <li class="mod result idx0 people hover" data-li-entity-id="354494011" data-li-position="0"> </li>

    </div>

    </br>


    <div id="results-container"><ol id="results" class="search-results">

        <li class="mod result idx0 people hover" data-li-entity-id="354494012" data-li-position="1"> </li>

    </div>
    </br>

    <div id="results-container"><ol id="results" class="search-results">

        <li class="mod result idx0 people hover" data-li-entity-id="354494022" data-li-position="2"> </li>

    </div>

</body>
</html>

    java.util.List<WebElement> links = (List<WebElement>) driver.findElements(By.linkText("element"));           
    System.out.println(links.size());

Page

7
  • Why can't you just start loop and call each element by it's index? Commented Feb 29, 2016 at 14:29
  • Because each one changes the page. Commented Feb 29, 2016 at 14:35
  • Sooooo...? What is the problem?:) You run for loop for each element in list that you get from find_elements() click on first link, do all your operations, then in next iteration you do all the same for next element and so on... Commented Feb 29, 2016 at 14:41
  • Have a look at this: stackoverflow.com/q/13448091/954442. It has a solution that also avoids you hitting the inevitable StaleElementException. Commented Feb 29, 2016 at 14:41
  • @Andersson I'm new to Selenium , you can give me an example of how to do this , please? Commented Feb 29, 2016 at 14:51

2 Answers 2

4
List<WebElement> links = driver.findElements(By.className("search-results"));
for( int i = 0; i < links.size(); i++)
{
    //The stop below is necessary to store all links in a list to access later.  
    links = driver.findElements(By.className("search-results")); 
    links.get(i).click();
    // Your code here
    driver.navigate().back();
}
Sign up to request clarification or add additional context in comments.

Comments

1

If the operations you need to perform are the same for all the pages you navigate to, you can use something like this:

List<WebElement> links = (List<WebElement>) driver.findElements(By.linkText("element"));           
for (WebElement link : links) 
{
    link.click();
    doWhateverOtherActions();
    driver.navigate().back();
    break();
}

If the actions on each page are different, you should consider identifying each link separately as a WebElement, and create methods for each to click on the link, do specific actions for that page and return to the initial page.

1 Comment

failed, to return to the page they click on the same link already clicked previously .

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.