0

i'm trying to create a selenium script in c# to check whether a URL is working or returning any error. What is the simplest way to do that.

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Abhishek Kalotra
  • 139
  • 1
  • 3
  • 12
  • 1
    simply try to find any object like href attribute . You can check this url https://www.swtestacademy.com/verify-url-responses-selenium/ – Shyam Feb 11 '20 at 08:42
  • 1
    Using Selenium is useless in that situation, you just have a lot of useless overhead. Use HttpClient as suggested by @Guy – Marco Salerno Feb 11 '20 at 09:32

2 Answers2

1

Don't do it with Selenium, use HttpClient

string url = "url";
var client = new HttpClient();
var checkingResponse = await client.GetAsync(url);
if (checkingResponse.IsSuccessStatusCode) {
    Console.WriteLine($"{url} is alive");
}
Guy
  • 46,488
  • 10
  • 44
  • 88
0

To check whether a URL is working or returning any error using Selenium's C# clients, you can simply use WebRequest and HttpWebResponse class to get the page response and status code as follows:

//Declare Webrequest
HttpWebRequest re = null;
re = (HttpWebRequest)WebRequest.Create(url);
try
{
    var response = (HttpWebResponse)re.GetResponse();
    System.Console.WriteLine($"URL: {url.GetAttribute("href")} status is :{response.StatusCode}");
}
catch (WebException e)
{
    var errorResponse = (HttpWebResponse)e.Response;
    System.Console.WriteLine($"URL: {url.GetAttribute("href")} status is :{errorResponse.StatusCode}");
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352