Paulo Roberto
Paulo Roberto

Reputation: 1548

Clicking on the next list element using Webdriver selenium - Java

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

Upvotes: 0

Views: 4957

Answers (2)

Shiva Krishna Chippa
Shiva Krishna Chippa

Reputation: 688

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();
}

Upvotes: 4

ami2110
ami2110

Reputation: 36

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.

Upvotes: 1

Related Questions