Question

I want to skip the loading of whole web page and just want to check status of URL in selenium webdriver so that the execution can be much faster.

Was it helpful?

Solution

If you just want to check status of a page and don't need to bother about content inside that, you can just get response code and check if it is 200(HTTP_OK). I would suggest you use simple java to verify instead of trying to get it done with selenium webdriver. I just wrote a small program that does just that. See it this works fine for you. This tests if the web page can be successfully reached and didn't send back any 404 or 500 or any other error.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpTest 
{
    public static void main(String[] args) throws IOException 
    {
        URL url = new URL ( "http://www.google.com/");
        HttpURLConnection conn =  ( HttpURLConnection )url.openConnection (); 
        conn.setRequestMethod ("HEAD");   // GET will fetch the content of the page, which is not needed for your case. 
        conn.connect () ; 

        if(conn.getResponseCode() == HttpURLConnection.HTTP_OK)
            System.out.println("Success");
    }

}

OTHER TIPS

What do you want to do? Do you want to click some links on a page and check whether they are redirecting to right URL? In this case you can try checking for change in URL. See the example below:

int i=0;
do{
    try {
    Thread.sleep(20);
    } catch (InterruptedException e) {
    }
    i++;
 } while(driver.getCurrentUrl().equals("The URL on which the page is originally.")&&i<10); //After URL changes, verify it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top