FindElement VS FindElements
Using FindElement we can find only one webelement, but when we use FindElements, we can find all the webelements in the page. It's like getting all the webelements in onelist for the particular tags.
How we will use an list in JAVA, the same way we can get the webelements in single list using the java.util.List<WebElement> findElements(By by) method.
It will return empty list if no return is found.
In practical, imagine that you are member of more than 100 groups in facebook. How will you get all the URL links in single list. In order to get all the URL's of the webpage, we can write the scripts in selenium.
Just try the below code for getting all the urls on the web page. In this example, I have used my youtube channel webpage, in this home page we can see more than one links, such as channel hyperlinks, subscribe links etc.,
To get all the hyperlinks, find the <a> tag element using the below code.
Input:
Input:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class google_getallurl { private static WebDriver driver = null; public static void main(String[] args) { // Create a new instance of the Firefox driver driver = new FirefoxDriver(); //Implicit wait, this method is used to wait for the webelement to load driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch youtube channel web Page driver.get("https://www.youtube.com/techpills"); //get the tagname for hyperlink and store it in variable list using //findElement method java.util.ListOutput:links = driver.findElements(By.tagName("a")); //Printing the link size in console which means total System.out.println(links.size()); //For loop to print each hyperlinks from the links list variable for (int i = 1; i<=links.size(); i=i+1) { //System.out.println(links.get(i).getText()); System.out.println("No."+i+ " " +links.get(i).getAttribute("href")); } } }