我对 Selenium C# 很陌生。我正在寻找使用 Selenium C# 查找网站损坏链接的方法。我可以找到一些 Java Selenium 的解决方案,但我找不到使用 Selenium C# 的方法。如果有人可以为相同的文件或任何文档的任何链接发布一小段代码,以便我可以参考和遵循它,那将非常有帮助。提前致谢。
您可以尝试遍历 'a' 标签列表并在 http 请求中检查 200 OK:
IList<IWebElement> links = driver.FindElements(By.TagName("a"));
foreach (IWebElement link in links)
{
var url = link.getAttribute("href");
IsLinkWorking(url);
}
bool IsLinkWorking(string url) {
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url);
//You can set some parameters in the "request" object...
request.AllowAutoRedirect = true;
try {
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("\r\nResponse Status Code is OK and
StatusDescription is: {0}", response.StatusDescription);
// Releases the resources of the response.
response.Close();
return true;
}
else
{
return false;
}
} catch { //TODO: Check for the right exception here
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1747 次 |
最近记录: |