How to find the number of broken pages in a particular page?

48 views
Skip to first unread message

Sudhansu Sekhar panda

unread,
May 15, 2013, 7:41:08 AM5/15/13
to seleniu...@googlegroups.com
Hi All,

How to find the number of broken pages in a particular page?

Can anyone help me for the same through code?

Thanks,

Sudhansu

ARK Satyanarayana Raju

unread,
May 15, 2013, 7:50:15 AM5/15/13
to seleniu...@googlegroups.com
Hi,
This is sample code for u r question.

 
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;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FindLinksandCheckforBrokenLinks {
    WebDriver driver;
    @BeforeTest
    public void open()
    {
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://www.google.co.in/");// Use navigate instead of driver.get()
    }
    @AfterTest
    public void teardown()
    {
        driver.quit();
    }
    @Test
    public void brokenlinks() throws InterruptedException
    {
          WebElement footer= driver.findElement(By.xpath("//div[@id='footer']"));  // Get Footer element which contains all footer links
          List<WebElement> elements=footer.findElements(By.tagName("a"));
          int size=elements.size();
          System.out.println("Total links are: "+size);
          for(int j = 0;j<size;j++){    //create loop based upon number of links to traverse all links
             footer= driver.findElement(By.xpath("//div[@id='footer']"));   // We are re-creating "footer" webelement as DOM changes after navigate.back()
             footer.findElements(By.tagName("a")).get(j).getText();
             footer.findElements(By.tagName("a")).get(j).click();
             Thread.sleep(3000);
             System.out.println(driver.getTitle());
              if(driver.getTitle().contains("404")) {
               System.out.println("404 Found");
              }
              driver.navigate().back();
           Thread.sleep(3000);
          }

    }     
    }
   

Thanks,
Raju

Mark Collin

unread,
May 16, 2013, 3:52:30 AM5/16/13
to seleniu...@googlegroups.com
This won't work, you will suffer from StaleElementReferenceExceptions after navigating to the first link and then clicking back.

I would suggest not using selenium to check (by all means use Selenium to find them, just not to check them) links but instead use a Java library that can do it instead.  Have a read of:

http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

Specifically the section called "Checking that links are valid"
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/d0e742fd-4838-4219-88e4-7f67017a751c%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sudhansu Sekhar panda

unread,
May 16, 2013, 7:57:14 AM5/16/13
to seleniu...@googlegroups.com
Hi Mark,

You are absolutely right. The same error message is getting displayed. Is there any way to prevent this?

Mark Collin

unread,
May 16, 2013, 9:07:46 AM5/16/13
to seleniu...@googlegroups.com
Yes, read the blog entry I linked It tells you how to do what you want to do.
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages