如何在webdriver中获取网页中的所有可用链接?
答案 0 :(得分:5)
您可以使用以下逻辑获取所有链接。
List<WebElement> link=driver.findElements(By.tagName("a"));
for(WebElement ele:link)
{
System.out.println(ele.getText());
}
答案 1 :(得分:1)
List<WebElement> list=driver.findElements(By.xpath("//a"));
System.out.println("No of links present="+ list.size());
// use of for loop for iteration
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getText());
}
System.out.println("-------------------------");
//use of for each for iteration
for(WebElement wb: list)
System.out.println(wb.getText());
答案 2 :(得分:1)
我同意Max.Mirkia,从'herf'获取网址
修改了Santoshsarma代码
List<WebElement> tagName=driver.findElements(By.tagName("a"));
for(WebElement ele:tagName)
{
String urls = ele.getAttribute("href");
System.out.println(urls);
}
}
答案 3 :(得分:0)
List<WebElement> list=driver.findElements(By.cssSelector("a"));
for(WebElement link:list)
{
System.out.println(link.getText());
}