Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Selenium WebDriver Count Total number of Web Link and All Element on Webpage

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 9.36k
    Comment on it

    Count Total number of Web Links and All Elements on Webpage:

     

    In this blog, we discuss about how to get the total number of web links and all elements on webpage.

     

    Steps to be automated:

     

    • Launch the web browser
    • Open the URL (eg. "http://google.com")
    • Identify the total number of Links on webpage and assign into Webelement List
    • Print the total count of links
    • Identify all the elements on web page
    • Count the total all element
    • Print the total count of all element

     

    WebDriver Code goes as below:

     

    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class countlinks {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		  WebDriver driver = new FirefoxDriver();
    		  driver.get("https://google.com");
    		  
    		  List<WebElement> links = driver.findElements(By.xpath("//a"));    //Identify the number of Link on webpage and assign into Webelement List 
    		  
    		  int linkCount = links.size();     // Count the total Link list on Web Page
    		  
    		  System.out.println("Total Number of link count on webpage = "  + linkCount);    //Print the total count of links on webpage
    		  
    		  List<WebElement> allElements = driver.findElements(By.xpath("//*"));      //Identify all the elements on web page
    		  
    		  int elementsCount = allElements.size();     //Count the total all element on web page
    		  
    		  System.out.println("Total Number of All Element on webpage = "  + elementsCount);         //Print the total count of all element on webpage
    		  
    		 		  
    	}
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: