How get all the elements inside the <a> tag in selenium

Viewed 35

How to locate all the elements inside the tag? and want to store it an List<WebElemnt tag is that posible. or how to store the list of elements

@Test (priority = 1)
public void Loading_Form() throws InterruptedException, AWTException
{
    WebElement glb = driver.findElement(By.xpath("//input[@id='globalSearchinp']"));
    sendkeys(driver, glb, 20, Formname);
    List<WebElement> findElements = driver.findElements(By.xpath("//a[@class='d-flex text-dark text-hover-primary align-items-center mb-5']"));
    System.out.println(findElements.size());
    new WebDriverWait(driver, Duration.ofSeconds(20))
    .until(ExpectedConditions.visibilityOfAllElements(findElements));
    System.out.println("Total Schemas "+ findElements.size());
    for (int i = 0; i < findElements.size(); i++) 
    {
        System.out.println(findElements.get(i).getText());
        if(findElements.get(i).getText().equals(findElements))
        {
            findElements.get(i).click();
            break;
        }
    }
}

HTML CODE

1 Answers
List<WebElement> listOfElements = driver.findElements(By.xpath("//div/a"));

Usage example (from this site):

package com.sample.stepdefinitions;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class NameDemo {

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://demo.guru99.com/test/ajax.html");
    List<WebElement> elements = driver.findElements(By.name("name"));
    System.out.println("Number of elements:" +elements.size());

    for (int i=0; i<elements.size();i++){
      System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
    }
  }
}
Related