0

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.

1
  • Is there an error message you are receiving when it fails to click on the second link?CommentedMar 7, 2018 at 22:00

2 Answers 2

4

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.

7
  • THIS WORDED THANKS!
    – Jonathan
    CommentedMar 8, 2018 at 0:06
  • Having another issue it is not looping back to click the drop down code is above
    – Jonathan
    CommentedMar 8, 2018 at 0:55
  • Stack trace is :org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
    – Jonathan
    CommentedMar 8, 2018 at 1:09
  • Any additional help is appreciated
    – Jonathan
    CommentedMar 8, 2018 at 2:14
  • 2
    @Jonathan, based on your original post: When you leave the page and come back, the WebElement is no longer linked. The mixed up way to accomplish what you're trying is: (1) Get the list so you know how many links you have (2) Start a loop with the count of the links (3) INSIDE the loop, get the list again (different variable) (4) Using the second variable as the link to click (5) Repeat the loop. Basically, you need to re-read in the links every time you come to the page so that they are not "stale". But you need to read the list in once so you know how many time to iterate the loop.
    – MivaScott
    CommentedMar 8, 2018 at 6:02
2

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 } 
1
  • This worked I can now follow what you did pic it apart practice coding it with other variations... thank you . I know i have come along way but i have so much more to learn.. Thanks again :)
    – Jonathan
    CommentedMar 8, 2018 at 17:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.