How to get columns of WebElement values outside the loop for comparison?

44 views
Skip to first unread message

Shreekant Mendhapurkar

unread,
Oct 7, 2015, 11:00:49 AM10/7/15
to Selenium Users

In below piece of code userID gives me the all column values of the current table, but when i try to use those value outside the loop for comparison to other values, it ask to create variable of colValue for class (though I used with List or String or WebElement under the loop). I need to use colValue outside the loop for comparison with the database values.


`
WebElement table = driver.findElement(By.xpath(".//*[@id='DataTables_Table_0']"));

              for(int i = 0; i<y; i++){
                     List<WebElement> allUserId = table.findElements(By.xpath("//td[1]"));
                     WebElement next1= driver.findElement(By.id("DataTables_Table_0_next"));
System.out.println("User ID = "+ userID.getText());
                           // String colValue = userID.getText();
                           }
                     next1.click();
              Thread.sleep(5000);
`

Shawn Knight

unread,
Oct 7, 2015, 1:26:49 PM10/7/15
to Selenium Users
why not make each row of the table a page-object? A class with predefined methods including one that conducts "matching" so you can search through a list of these page-objects and find the one you are looking for. I have a blog post and example project (in Java) around this concept.

Shreekant Mendhapurkar

unread,
Oct 8, 2015, 10:42:48 AM10/8/15
to Selenium Users
Thanks Shawn, 

I used the code for about as below and it working fine where nxtValue is used as total iteration need to click on Next button where one screen gives me 10 records and so on.


ArrayList<String> uservalue = new ArrayList<String>();

WebElement table = driver.findElement(By.xpath(".//*[@id='DataTables_Table_0']"));

int i =0;           

for(i = 0; i<= nxtValues; i++){

       List<WebElement> allUserId = table.findElements(By.xpath("//td[1]"));

       WebElement next1= driver.findElement(By.id("DataTables_Table_0_next"));

       for (WebElement userID : allUserId){

//            System.out.println("User ID = "+userID.getText());

              uservalue.add(userID.getText());

}

next1.click();

Thread.sleep(5000);

}   

System.out.println("\nColValues: "+uservalue);



Regards,

Shreekant 

Shawn Knight

unread,
Oct 8, 2015, 12:27:26 PM10/8/15
to Selenium Users
Have you considered using a do/while loop for pagination? Here is an example from one of my projects -- 


do
{
Optional<LibraryTitle> libraryTitle = getLibraryTitles().stream()
.filter(libraryTitle1 -> libraryTitle1.isMatch(epub.getTitle()))
.findFirst();

if (libraryTitle.isPresent())
return addAndViewInReader(libraryTitle.get());

if (isNextButtonDisabled())
break;
else
navigateToNextPage();

} while (true);
Reply all
Reply to author
Forward
0 new messages