If you lot remember, Earlier nosotros learnt how to extract all links from page In THIS POST. Extracting all links from page Is non useful If you lot don't know all the links are working fine or simply about of them are broken links or supposing at that spot are few broken Images links. How to find these broken links or broken Images from page using selenium WebDriver? This Is constituent of testing In which you lot involve to
check condition of links/Images -> 1) Link URLs are opening targeted page 2) Images display properly on page or not. If links are Incorrect hence It volition non work. Finding each too every link from page too verifying It manually volition accept lots of your time. You volition respect many broken link checker tools online. You tin perform same draw of piece of occupation using selenium WebDriver. Lets encounter instance on finding broken links from unmarried page.
In bellow given example, First of all I get got calculated full number of links on page. Then extracted all links ane yesteryear ane too cheque Its reply code yesteryear calling getResponseCode function. I get got used apache Interface HttpResponse to become the reply code of URL. If It Is 200, that agency link URL Is non broken too working fine. But If response code Is 404 or 505 that agency link or Image IS broken.
In bellow given example, I get got used assay page where ane link too Img URL Is broken to demonstrate you lot practically how It volition differentiate those links from valid links. Execute bellow given selenium WebDriver assay instance In your eclipse too verify number In console. Console number volition demonstrate you lot condition of link URL If It Is broken or not.
package Testing_Pack; import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; populace degree BrokenlinksTest { populace static void main(String[] args) throws IOException { WebDriver driver = novel FirefoxDriver(); driver.manage().window().maximize(); driver.get(" "); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Find full No of links on page too impress In console. List<WebElement> total_links = driver.findElements(By.tagName("a")); System.out.println("Total Number of links flora on page = " + total_links.size()); //for loop to opened upward all links ane yesteryear ane to cheque reply code. boolean isValid = false; for (int i = 0; i < total_links.size(); i++) { String url = total_links.get(i).getAttribute("href"); if (url != null) { //Call getResponseCode business office for each URL to cheque reply code. isValid = getResponseCode(url); //Print message based on value of isValid which Is returned yesteryear getResponseCode function. if (isValid) { System.out.println("Valid Link:" + url); System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); System.out.println(); } else { System.out.println("Broken Link ------> " + url); System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); System.out.println(); } } else { //If <a> tag produce non comprise href attribute too value hence impress this message System.out.println("String null"); System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); System.out.println(); continue; } } driver.close(); } //Function to become reply code of link URL. //Link URL Is valid If flora reply code = 200. //Link URL Is Invalid If flora reply code = 404 or 505. populace static boolean getResponseCode(String chkurl) { boolean validResponse = false; endeavor { //Get reply code of URL HttpResponse urlresp = novel DefaultHttpClient().execute(new HttpGet(chkurl)); int resp_Code = urlresp.getStatusLine().getStatusCode(); System.out.println("Response Code Is : "+resp_Code); if ((resp_Code == 404) || (resp_Code == 505)) { validResponse = false; } else { validResponse = true; } } grab (Exception e) { } provide validResponse; } }
Console output for to a higher house instance execution volition looks similar bellow.
This way you lot tin respect broken links or Images from whatsoever page using selenium WebDriver.