html - 如何列出并单击网页中找到的所有链接以及如何检查该链接是否重定向到 404 页面并在 selenium 中引发错误?
问题描述
请检查我尝试过的以下代码在这里我想检查是否所有链接都已打开且不应包含任何 404 页面
public void alllinks() {
suites.setupEnviroment();
WebDriver driver = suites.getWebDriver();
driver.get(suites.WEB_PATH5);
Dimension d = new Dimension(1455, 900);
driver.manage().window().setSize(d);
try {
List<WebElement> links = driver.findElements(By.tagName("a"));
ArrayList<String> targets = new ArrayList<String>();
// collect targets locations
for (WebElement link : links) {
targets.add(link.getAttribute("href"));
}
for (String target : targets) {
driver.get(target);
try {
((WebDriver) links).getPageSource().contains("404");
} catch (Exception e) {
System.out.println("error");
}
// do what is needed in the target
}
Logger.getLogger("results").log(new LogRecord(Level.INFO,
MethodHandles.lookup().lookupClass().getCanonicalName() != null ? "success" : "failure"));
driver.close();
} catch (Exception e) {
Logger.getLogger("results").log(new LogRecord(Level.INFO,
MethodHandles.lookup().lookupClass().getCanonicalName() == null ? "success" : "failure"));
}
提前致谢!
解决方案
这个样本应该可以完成这项工作。根据您的需要进行调整。
public class FindBrokenLinks {
private WebDriver driver;
private int invalidLinks = 0;
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.get("http://google.com"); // change the url
}
@Test
public void checkForBrokenLinks() {
try {
List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
if (link != null) {
checkLink(link);
}
}
System.out.println("Total broken links: " + invalidLinks);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() {
if (driver != null)
driver.quit();
}
public void checkLink(WebElement linkElement) throws IOException {
HttpURLConnection connection = null;
try {
String link = linkElement.getAttribute("href");
URL url = new URL(link);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
// change the code for your needs.
if (responseCode == 404) {
// you can trow error also ...
System.out.println("Found invalid link: " + link);
invalidLinks++;
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
if (connection != null) {
connection.getErrorStream().close();
}
}
}
}
推荐阅读
- tinymce - 弹出窗口的 TinyMCE 焦点问题
- c++ - 如何在组合函数中使用 boost::asio::defer()?
- wordpress - 如何获取当前国家的 tag_id 链接?
- angular - 可以在 Angular 服务构造函数中调用 HTTP 端点吗?
- ssl - 在 JHipster 中配置 SSL
- xamarin.forms - 可绑定属性更新未使用自定义渲染
- javascript - javascript 日期选择器未激活
- node.js - 节点 js 显示不正确的时区
- javascript - 建议框应用不会显示建议
- c# - 在 C# 控制台应用程序中获取 google 驱动程序中文件的 downloadUrl