Created
October 3, 2016 12:42
-
-
Save executeautomation/d339d5ba35adeb46594fca1462d08957 to your computer and use it in GitHub Desktop.
Identifying broken links with Selenium C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using OpenQA.Selenium.Firefox; | |
using System; | |
using System.Linq; | |
using System.Net; | |
namespace SeleniumParallelTest | |
{ | |
class BrokenLinks : Base | |
{ | |
static void Main(string[] args) | |
{ | |
Driver = new FirefoxDriver(); | |
Driver.Navigate().GoToUrl("http://www.executeautomation.com/blog"); | |
//Declare Webrequest | |
HttpWebRequest re = null; | |
var urls = Driver.FindElements(By.TagName("a")).Take(10); | |
Console.WriteLine("Looking at all URLs of ExecuteAutomation site :"); | |
//Loop through all the urls | |
foreach (var url in urls) | |
{ | |
if (!(url.Text.Contains("Email") || url.Text == "")) | |
{ | |
//Get the url | |
re = (HttpWebRequest)WebRequest.Create(url.GetAttribute("href")); | |
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}"); | |
} | |
} | |
} | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apologies didn't realize that replying to a thread that has someone tagged auto triggers. Thanks for bringing it to my attention