7
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()) {
    System.out.println(itr.next());
}

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]
  • 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[@class='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span[contains(text(), "LISTVALUE")] to perform click action – STE Mar 13 '14 at 15:42
8

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" ) );
    dropDown.click();
    List<WebElement> allOptions = dropDown.findElements(By.xpath(".//option"));
    for ( WebElement we: allOptions) { 
        dropDown.sendKeys( Keys.DOWN ); //simulate visual movement
        sleep(250);       
        if ( we.getText().contains( text ) ) select.selectByVisibleText("Value1");
    }
}
  • What is the reason for using sleep() method? – dzieciou Mar 16 '14 at 9:07
  • 1
    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 '14 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 '14 at 16:40
  • 1
    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 '14 at 0:00
3

@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();
    System.out.println(row.getText());
}

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();
    System.out.println(row.getText());
} 
  • 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/div/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 '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 '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 '14 at 12:23
3

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

Iterator<WebElement> itr = allLinks.iterator();
while(itr.hasNext()) {
    System.out.println(itr.next().getText());
}
  • Okay, this question was asked ages ago and i saw this now – Octavian Rox Sep 12 '14 at 21:41
  • 1
    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 '14 at 21:40
  • might ListIterator be more specific? Imaginary bonus points for using hasNext. – Thufir Dec 29 '18 at 18:42
0
 List<WebElement> elements = driver.findElements(By.xpath("//ul[@id='ctl00_MainContent_view1Extender_SearchBarAutoComplete2_2_completionListElem']//li"));
    java.util.Iterator<WebElement> program = elements.iterator();
    while (program.hasNext()) {
        String values = program.next().getText();

        if(!values.equals("null"))
        {
            testStepPassed("Field should display all the Programs defined for the provider " +values);
        }
        else
        {
            testStepFailed("Field is not display all the Programs defined for the provider ");
        }

    }
  • What does this add to the existing and accepted answers? For an answer to an old question it helps to add more than just code. – Kate Paulk Feb 24 '16 at 16:47
  • Hey there JaiHind! what does this code do? how is this better than the accepted answer? – IAmMilinPatel Feb 25 '16 at 1:33
0

Add System.out.println(itr.next().getAttribute("src"));

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()) {
    System.out.println(itr.next()**.getAttribute("src")**);

}

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