Your ultimate guide

Find Elements

findElements() Method:
    findElements method finds the list of web elements on the web page. It returns an empty list if no elements are found.

Syntax:
    List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));


Example:
class Example
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.chorme.driver", "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://testingcolleges.blogspot.com");
        
        List<WebElement> allLinks = driver.findElements(By.tagName("a"));
        //print all the links (output: visible text -> Link)
        for(WebElement testing:allLinks)
        {
            System.out.println(testing.getText() + " -> " + testing.getAttribute("href"));
        }
        driver.close();
}


No comments:

Post a Comment