Quantcast

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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sudhansu Sekhar panda
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/91f6a66c-f88e-4232-880d-b004a8bff90c%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ARK Satyanarayana Raju
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


On Wednesday, May 15, 2013 5:11:08 PM UTC+5:30, Sudhansu Sekhar panda wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mark Collin
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"

On 15/05/2013 12:50, ARK Satyanarayana Raju wrote:
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


On Wednesday, May 15, 2013 5:11:08 PM UTC+5:30, Sudhansu Sekhar panda wrote:
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
--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
 
 

--
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 [hidden email].
To post to this group, send email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sudhansu Sekhar panda
In reply to this post by Sudhansu Sekhar panda
Hi Mark,

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

On Wednesday, 15 May 2013 17:11:08 UTC+5:30, Sudhansu Sekhar panda wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c520f072-254f-4aea-8b66-7f3800d9854d%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mark Collin
Yes, read the blog entry I linked It tells you how to do what you want to do.


On 16/05/2013 12:57, Sudhansu Sekhar panda wrote:
Hi Mark,

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

On Wednesday, 15 May 2013 17:11:08 UTC+5:30, Sudhansu Sekhar panda wrote:
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
--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c520f072-254f-4aea-8b66-7f3800d9854d%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
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 [hidden email].
To post to this group, send email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Loading...