Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.
List<WebElement> allLinks = driver.findElements(By.xpath("//div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span"));

Iterator<WebElement> itr = allLinks.iterator();
while(itr.hasNext()) {

I want to print the list elements one by one and perform some actions. But I am unable to get the value present in the list.

This is what I am getting in console:

[[FirefoxDriver: firefox on MAC (81e15827-9357-0341-9c72-5b26054f780d)] -> xpath: //div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span]

share|improve this question
Print the first value in list. It should be placed instead of LISTVALUE in the following xpath //div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@cla‌​ss='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span[contains(text(), "LISTVALUE")] to perform click action –  STE Mar 13 at 15:42

3 Answers 3

Here is a good example of how I select from a dropdown menu:

public Boolean selectByText( String text ) {
    WebElement dropDown = driver.findElement( By.xpath( ".//dropdown/path" ) );
    List<WebElement> allOptions = dropDown.findElements(By.xpath(".//option"));
    for ( WebElement we: allOptions) { 
        dropDown.sendKeys( Keys.DOWN ); //simulate visual movement
        if ( we.getText().contains( text ) ) select.selectByVisibleText("Value1");
share|improve this answer
What is the reason for using sleep() method? –  dzieciou Mar 16 at 9:07
When you are simulating the experience of a user doing key-downs on a dropdown menu, you don't want it to go so fast that you can see it. –  djangofan Mar 16 at 16:34
Ahh, so this is just for demoing. I understand this will work also without sleeping, so that when running x tests ( x >1000), their execution time is not unnecessary prelonged. –  dzieciou Mar 16 at 16:40
That's right. When I make tests I record video and I run multi-threaded and so speed matters not as much to me. Therefore this technique is really useful for debugging by using video playback. –  djangofan Mar 17 at 0:00

@sathiya: What kind of list you want to iterate (Dropdown..etc)?

Below is the method to get data from the table. Hope this will help you.

WebElement table = driver.findElement(By.xpath("your path"));
List<WebElement> rows = table.findElements(By.tagName("your tagName"));
java.util.Iterator<WebElement> i = rows.iterator();
while(i.hasNext()) {
    WebElement row = i.next();

For dropdown list iterate you can use below code.

List<WebElement> drop = driver.findElements(By.name("customerId"));
java.util.Iterator<WebElement> i = drop.iterator();
while(i.hasNext()) {
    WebElement row = i.next();
share|improve this answer
For Dates that are inside a table. Xpath for table-/html/body/div[4]/div/div[2]/div[3]/div/div[5]/div[2]/div/ul/li/div/div/di‌​v/table/tbody/tr/td/table. I dont want all the rows to get printed, because only the future dates will be available in the page. Common Xpath for the dates will be-"//div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class‌​='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span". How to print the values for this case. –  STE Mar 14 at 10:07
@sathiya Could you provide the url of application or give sample application so we can work around. And please elaborate your query. When i used above code in my application i got all the dates (Which all are displayed). Result : 'Mo Tu We Th Fr Sa Su 24 25 26 27 28 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6' –  QA4it Mar 14 at 12:05
List myList = myDriver.findElement(By.xpath("")); System.out.println(myList.size()); for(int i =0; i< myList.size();i++){ System.out.println(myList.get(i).getText()); } Using this code, i can print all the values that are needed say 17 to 31 of Feb month.On using myList.get(i).click(); after printing the first element, test case fails completely. All I need to do is Print the first element, click it and check for condition. On failure do increment. Print second element, click it and check for condition. –  STE Mar 14 at 12:23

It is giving you the web element just put .getText() whenever you see such code.

share|improve this answer
Okay, this question was asked ages ago and i saw this now –  Octavian Rox Sep 12 at 21:41
That's fine, there's never a bad time for a great answer. The question is - is this a great answer? I feel like it could use more love before it's a great answer. =) –  corsiKa Sep 30 at 21:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.