• Selenium Video Tutorials

Selenium - 查詢所有連結



Selenium Webdriver 可用於計算 Web 應用程式上的連結總數。通常,在執行網頁抓取時,通常需要計算連結。所有連結(稱為超連結)都具有名為anchor 的標籤名,並且它具有一個稱為 href 的屬性。

HTML 中連結的識別

現在讓我們討論一下下圖所示網頁上“首頁”連結的識別。開啟 Chrome 瀏覽器,右鍵單擊網頁,然後單擊“檢查”按鈕。然後,該頁面的完整 HTML 程式碼將可用。要檢查頁面上的“首頁”連結,請單擊位於可見 HTML 程式碼頂部的左上方箭頭,如下所示。

Selenium Links 1

單擊並將箭頭指向首頁超連結後,其 HTML 程式碼將可見,同時反映錨標籤名(稱為“a”)和包含頁面連結的 href 屬性。

<a href="https://tutorialspoint.tw/index.htm" target="_blank">Home</a>
Selenium Links 2

計算總連結數

讓我們以以上頁面為例,我們將在其中計算總連結數。在此頁面上,總連結數應為 42。

示例

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://tutorialspoint.tw/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 依賴項

<?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

在上面的示例中,我們計算了網頁上的總連結數,並在控制檯中收到訊息 - 頁面上的總連結數:42

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

獲取所有連結名稱

現在,讓我們來看一個獲取上面討論的網頁上所有超連結名稱的示例。

示例

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://tutorialspoint.tw/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

在上面的示例中,我們計算了網頁上的總連結數,並在控制檯中收到訊息 - 總連結數:42 和所有連結文字

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

結論

本教程全面介紹了 Selenium Webdriver 查詢所有連結。我們從描述 HTML 中連結的識別開始,並透過示例說明如何在 Selenium Webdriver 中計算總連結數和連結名稱。這使您能夠深入瞭解 Selenium Webdriver - 查詢所有連結。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告
© . All rights reserved.