Jonathan
Jonathan

Reputation: 395

Loop through Web Elements and Click each link

Hi my code works My goal is to loop through each element in the table and and click the element so i can go to the next page basically I want to do this

1 click element form drop down
2 //code some stuff
3 Go back 
4 click next element from drop down 
5 code some stuff and etc

My code:

List<WebElement> CCTable = driver.findElements(By.id("companyIdBarCompListGrid_rows_scrollpane"));
     for ( WebElement client: CCTable) { 
         System.out.println("\n"+client.getText().substring(0, 20)+"\n");
        client.click();
        Thread.sleep(10000);

    }

enter image description here

My problem is that its finding and printing out the text in my **console in eclipse ** but it is not clicking on the link or each link.

I am trying to follow the suggestions here https://sqa.stackexchange.com/questions/12790/how-to-iterate-over-a-collection-of-items-in-selenium-webdriver but no success any help would be appreciated.

If I am doing something wrong please help me. Any help would be appreciated


PP_OBJ_CycleData.CCdropdown(driver).click();

    List<WebElement> CCTable = driver.findElements(By.cssSelector("div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"));
        // inner 4 loop 
        for ( WebElement ccode: CCTable) { 

             System.out.println("\n"+ccode.getText().substring(0, 20)+"\n");
             System.out.println("Number of links: " + CCTable.size());
             Thread.sleep(3000);

             ccode.click();



Thread.sleep(5000);

driver.findElement(By.xpath("//*[@id='companyIDBarContentPane']//span"));

            }// End Inner 4 loop



    PP_OBJ_CycleData.ReturnToSupport(driver);

Also this is in eclipse console:

enter image description here

Seems that the get text is not getting the other company code name. and it not looping through again after it clicks the first name and i click i get org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document.

Upvotes: 0

Views: 7124

Answers (2)

MivaScott
MivaScott

Reputation: 1806

Here is a working loop:

    String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a";
    int links = driver.findElements(By.cssSelector(selector)).size();
    System.out.println("Number of links: " + links);

    for (int i = 0; i < links; i++) {
        List<WebElement> CCTable = driver.findElements(By.cssSelector(selector));
        WebElement client = CCTable.get(i);
        System.out.println("\n"+client.getText().substring(0, 20)+"\n");
        client.click();

        // Things happen here

    }

Upvotes: 2

MivaScott
MivaScott

Reputation: 1806

The problem is you're identifying the wrong thing

The ID points to the DIV. And there is just one DIV; nothing to iterate. And while the DIV is clickable, clicking the DIV does nothing.

Instead, you need to find the A tags. This can be very difficult in a table; and all the IDs aren't really helping.

Basically you want something to the effect of:

List<WebElement> CCTable = driver.findElements(By.cssSelector("div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a");

You may have to play with it. You can add this line right after the List declaration:

System.out.println("Number of links: " + CCTable.size());

When it matches your expectations, you've found the right findElements string.

Upvotes: 4

Related Questions