Selenium 简明教程
Selenium - Find All Links
Selenium Webdriver 可用于计算 Web 应用程序上的链接总数。通常,在执行网页抓取时,我们通常需要计算链接。所有链接(称为超链接)都通过名为 * anchor* 的标签名,并且它有一个称为 href 的属性。
Selenium Webdriver can be used to count the total number of links on a web application. Often, while performing scrapping of a webpage, we would normally need to count the links. All links (referred to as hyperlinks) have by the tagname called anchor and it has an attribute known as the href.
Identification of Links in HTML
现在让我们讨论在以下图片中显示的网页上标识链接 Home 。打开 Chrome 浏览器,右键单击网页,然后单击“检查”按钮。然后,该页面的完整 HTML 代码将可用。要检查页面上的主页链接,请单击可见 HTML 代码顶部提供的左向上箭头,如下所示。
Let us now discuss the identification of the link Home on a webpage shown in the below image. Open the Chrome browser, right click on the web page, and then click on the Inspect button. Then, the complete HTML code for the page would be available. For investigating the Home link on the page, click on the left upward arrow, available to the top of the visible HTML code as highlighted below.
点击并指向超链接 Home 后,可以看到其 HTML 代码,既反映锚标签名(称为“a”),也反映包含页面链接的 href 属性。
Once, we had clicked and pointed the arrow to the Home hyperlink, its HTML code would be visible, both reflecting the anchor tagname (referred to as ‘a’), and the href attribute containing the link to a page.
<a href="https://www.tutorialspoint.com/index.htm" target="_blank">Home</a>
Count Total Links
让我们以以上页面为例,在此页面上,我们将计算链接总数。在此页面上,链接总数应为 42。
Let us take an example of the above page, where we would count the total number of links. Here on this page, the total number of links should be 42.
Example
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EgeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class CountLinks {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver() ;
// adding implicit wait of 10 secs
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Opening the webpage where we will count the links
driver.get( "https://www.tutorialspoint.com/selenium/practice/links.php" ) ;
// Retrieve all links using locator By.tagName a and storing in List
List<WebElement> totalLnks = driver.findElements(By.tagName("a"));
System.out.println( "Total number of links:" + totalLnks.size() ) ;
// Closing browser
driver.quit();
}
}
添加到 pom.xml 中的 Maven 依赖项
Maven dependencies added to pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
</dependencies>
</project>
Total number of links on a page: 42
Process finished with exit code 0
在以上示例中,我们已经计算了网页上的链接总数,并且在控制台中收到了消息 - Total number of links on a page: 42 。
In the above example, we had counted the total number of links on a web page, and received the messages in the console - Total number of links on a page: 42.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Get All Link Names
现在,让我们来看一个获取上面讨论的网页上所有超链接名称的示例。
Now, let us take a look at an example of getting the names of all the hyperlinks on the web page discussed above.
Example
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class TotalLinksWithNm {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver() ;
// adding implicit wait of 10 secs
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Opening the webpage where we will count the links
driver.get( "https://www.tutorialspoint.com/selenium/practice/links.php") ;
// Retrieve all links using locator By.tagName and storing in List
List<WebElement> totalLnks = driver.findElements(By.tagName("a"));
System.out.println( "Total number of links: " + totalLnks.size() );
// Running loop through list of web elements
for ( int j = 0; j < totalLnks.size() ; j++) {
// get the all hyperlink texts
System.out.println( "Link text is: " + totalLnks.get(j).getText() ) ;
}
// Closing browser
driver.quit();
}
}
Total number of links: 42
Link text is:
Link text is: Selenium Tutorial
Link text is: Text Box
Link text is: Check Box
Link text is: Radio Button
Link text is: Web Tables
Link text is: Buttons
Link text is: Links
Link text is: Broken Links - Images
Link text is: Upload and Download
Link text is: Dynamic Properties
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is:
Link text is: Home
Link text is: HomewPWPU
Link text is: Created
Link text is: No Content
Link text is: Moved
Link text is: Bad Request
Link text is: Unauthorized
Link text is: Forbidden
Link text is: Not Found
Process finished with exit code 0
在以上示例中,我们已经计算了网页上的链接总数,并且在控制台中收到了消息 - Total number of links: 42 and all the link texts 。
In the above example, we had counted the total number of links on a web page, and received the message in the console - Total number of links: 42 and all the link texts.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
At last, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Conclusion
这总结了我们对 Selenium Webdriver 查找所有链接教程的全面介绍。我们从描述 HTML 中的链接识别开始,并通过示例来说明如何在 Selenium Webdriver 中计算链接和链接名称的总数。这使您对 Selenium Webdriver - 查找所有链接有了深入的了解。明智的做法是继续练习你学到的知识并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Find All Links. We’ve started with describing identification of links in HTML, and examples to illustrate how to count total number of links and link names in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver - Find All Links. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.