Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Access all links simultaneously and checks whether links are working or not present within the Web page

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 916
    Comment on it

    Dear user,

    In the last blog, I wrote about how to access links using exact match and partial match in a web page through selenium web driver.

    Now in this blog, I will guide you about how to access all links present with-in the page are working using selenium web driver. This can be done using Java for-each loop and the By.tagName("a") method.

    The below WebDriver code checks each link from the web page and determines those that are working or not and those are still under development.

    package misbah;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    
    public class All_Links {
    
    public static void main(String[] args) throws InterruptedException{
      String baseUrl = "http://newtours.demoaut.com/";
      WebDriver driver;
      System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\Eclipse New setup(Neon)\\geckodriver.exe"); //provide the path of gecko driver for above selenium 3.0 versions
    
      driver= new FirefoxDriver();
      String underConsTitle = "Under Construction: Mercury Tours";
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    
      driver.get(baseUrl);
      List<WebElement> linkElements = driver.findElements(By.tagName("a"));
      String[] linkTexts = new String[linkElements.size()];
      int i = 0;
    
    
      //extract the link texts of each link element
      for (WebElement e : linkElements) {
       linkTexts[i] = e.getText();
    
       i++;
      }
    
      //test each link
    
      for (String t : linkTexts) {
       try{
        driver.findElement(By.linkText(t)).click();
        Thread.sleep(3000);
       }catch(Exception e)
       {
    
        driver.navigate().back();
    
       }
       if (driver.getTitle().equals(underConsTitle)) {
        System.out.println("\"" + t + "\""
          + " is under construction.");
    
       } else {
        System.out.println("\"" + t + "\""
          + " is working.");
       }
    
      }
    
      driver.quit();
    
     }
    

     

    Output:

    "Home" is working.

    "Flights" is working.

    "Hotels" is under construction.

    "Car Rentals" is under construction.

    "Cruises" is working.

    "Destinations" is under construction.

    "Vacations" is under construction.

    "SIGN-ON" is working.

    "REGISTER" is working.

    "SUPPORT" is under construction.

    "CONTACT" is under construction.

    "your destination" is under construction.

    "featured vacation destinations" is working.

    "Register here" is working.

    "Business Travel @ About.com" is under construction.

    "Salon Travel" is working.

     

    In the above code web driver creates a list of all links and matches with either link is working or is under construction.

     

 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: