How to iterate a list of WebElements and print the values one by one to perform some action



  • List allLinks = driver.findElements(By.xpath("//div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class='' or @class='datepickerSaturday' or @Pearlaqua 
    Iterator 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]
    


  • 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 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");
        }
    }
    


Suggested Topics