使用Java查找网页上所有链接的功能? [英] Function to find all the links on a webpage using java?

查看:79
本文介绍了使用Java查找网页上所有链接的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网页上找到损坏的链接,并且正在使用:

I am trying to find broken links on a webpage and I am using:

List<WebElement> var = driver.findElements(By.tagName("a")); 

只能在当前网页上找到元素,但我也想在第一页上找到可用的子元素.有什么办法吗?如果您能帮助我,我将不胜感激.预先感谢.

which only finds elements on a current web page but I also want to find sub elements available on the first page. Is there any way to do this? I would appreciate if you guys can help me out. Thanks in advance.

推荐答案

所有链接都有锚标记,因此您可以基于锚标记定位器创建Web元素列表,然后打印文本或href标记(即链接网址). 脚本-

All the links have anchor tag, so you can create a list of web elements based on the anchor tag locator and then print the text or the href tag(i.e. link URL) of each element. Script-

public static void main(String[] args){
    WebDriver driver = new FirefoxDriver();

    //Launching sample website
    driver.get("{webpage URL}");

    //Get list of web-elements with tagName  - a
    List<WebElement> allLinks = driver.findElements(By.tagName("a"));

    //Traversing through the list and printing its text along with link address
    for(WebElement link:allLinks){
        System.out.println(link.getText() + " - " + link.getAttribute("href"));
    }
}

参考-获取所有链接在使用硒和Java的网页上

这篇关于使用Java查找网页上所有链接的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆