SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Get/Extract All Links From Web Page Using Selenium WebDriver

As we all knows, Each and every software web application contains many number of different links/URLs. Some of them are redirecting to some page of same website and others are redirecting to any external software web application. One of my blog reader has requested the webdriver software test script example for extracting all these links from page and print it in console using selenium webdriver. I have created and published many different WEBDRIVER EXAMPLE SCRIPTS on this blog but requested example by blog reader is not pushed till now so I have created example for the same to share with all of you.

You can look at example of EXTRACTING TABLE DATA USING SELENIUM WEBDRIVER.

We have seen HOW TO USE findElements in webdriver to locate multiple elements of software web page. We can use same thing here to locate multiple links of the page. Bellow given example will retrieve all URLs from the software web page and will print all of them in console. You can click on each URL if you wish to do so. Try bellow given example for different websites by providing that website's URL in @Before annotation.

Copy bellow given @Test method part of extract all links from software web page and replace it with the @Test method part of example given on THIS PAGE.(Note : @Test method is marked with pink color in that linked page).

@Test
  public void test () throws InterruptedException 
  {
  try {
    List<WebElement> no = driver.findElements(By.tagName("a"));
    int nooflinks = no.size(); 
    System.out.println(nooflinks);
    for (WebElement pagelink : no)
         {
          String linktext = pagelink.getText();
          String link = pagelink.getAttribute("href"); 
          System.out.println(linktext+" ->");
          System.out.println(link);
          }
   }catch (Exception e){
             System.out.println("error "+e);
         }
           
  }

THIS LINK will show you many other basic action command examples. Have you READ POST on how to find broken link from page?

5 comments:

  1. Thanks for very helpful note.

    But, how could i clicking all links one by one after extracting??

    ReplyDelete
  2. Now that you have the all the link texts you can search for all of them with findElement(By.linkText()) method and click on all the links.

    ReplyDelete
  3. I did not understand for (WebElement pagelink : no)? What is the usage of :....can you please add comment for all the steps and explain the functionality. I am new to Selenium.

    ReplyDelete
    Replies
    1. This is advanced for loop
      use can use
      for(i=0;i<links.size();i++)
      {

      }

      Delete
  4. for(WebElement pagelink : no) - This is same as for for loop used to iterate over each and every elements of List no

    ReplyDelete