在我想测试网页中特定链接的导航的情况下,我遇到了一些问题,但是有许多链接具有相同的链接文本。我尝试了下面的代码
driver.get(baseUrl);
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
System.out.println ("No of links in page = "+linkElements.size());
ArrayList<String> hrefs = new ArrayList<String>();
for (WebElement a : linkElements) {
System.out.println(a.getText());
System.out.println(a.getAttribute("href"));
hrefs.add(a.getAttribute("href"));
}
int i=0;
for (String href : hrefs) {
driver.navigate().to(href);
String title = "404 Not Found";
if (driver.getTitle().equals(title)) {
System.out.println("\"" + href + "\" is navigating to error page");
} else {
System.out.println("\"" + href + "\" is working.");
}
}
它需要所有链接并导航。但我想导航到页面中的一些链接。请帮助:)