title: How to find broken links using Selenium WebDriver with Java
body: <p>I want to verify broken links on a website and I am using this code: </p>
  
  <pre><code> public static int invalidLink;
      String currentLink;
      String temp;
  
      public static void main(String[] args) throws IOException {
          // Launch The Browser
          WebDriver driver = new FirefoxDriver();
          // Enter URL
          driver.get("http://www.applicoinc.com");
  
          // Get all the links URL
          List&lt;WebElement&gt; ele = driver.findElements(By.tagName("a"));
          System.out.println("size:" + ele.size());
          boolean isValid = false;
          for (int i = 0; i &lt; ele.size(); i++) {
  
              isValid = getResponseCode(ele.get(i).getAttribute("href"));
              if (isValid) {
                  System.out.println("ValidLinks:" + ele.get(i).getAttribute("href"));
                  driver.get(ele.get(i).getAttribute("href"));
                  List&lt;WebElement&gt; ele1 = driver.findElements(By.tagName("a"));
                  System.out.println("InsideSize:" + ele1.size());
                  for (int j=0; j&lt;ele1.size(); j++){
                      isValid = getResponseCode(ele.get(j).getAttribute("href"));
                      if (isValid) {
                          System.out.println("ValidLinks:" + ele.get(j).getAttribute("href"));
                      }
                      else{
                          System.out.println("InvalidLinks:"+ ele.get(j).getAttribute("href"));
                      }
                  }
  
                  } else {
                      System.out.println("InvalidLinks:"
                              + ele.get(i).getAttribute("href"));
                  }
  
              }
          }
      }
  
  
      public static boolean getResponseCode(String urlString) {
          boolean isValid = false;
          try {
              URL u = new URL(urlString);
              HttpURLConnection h = (HttpURLConnection) u.openConnection();
              h.setRequestMethod("GET");
              h.connect();
              System.out.println(h.getResponseCode());
              if (h.getResponseCode() != 404) {
                  isValid = true;
              }
          } catch (Exception e) {
  
          }
          return isValid;
      }
  
  }
  </code></pre>