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(); | |
} | |
} | |
} |
I don't think there is any method called Take() on FindElements, @karthik any comments from your side?
I don't think there is any method called Take() on FindElements, @karthik any comments from your side?
.Take()
comes for System.Linq
since FindElements
returns you a collection (List) of elements, when you use Take(10)
, you are saying get me 10 item from the list of elements from the beginning of the list to 10. If you don't use take, it will simply return you all the elements found with By.TagName("a")
Stop tagging me. I'm not the person you are looking for.
Apologies didn't realize that replying to a thread that has someone tagged auto triggers. Thanks for bringing it to my attention
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the above code, what does .Take() imply?