- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵點選
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 錄製和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium 和其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
Selenium WebDriver - 處理連結
Selenium Webdriver 可用於處理網頁上的連結。在HTML術語中,每個連結(稱為超連結)都由名為 anchor 的標籤名標識。此外,網頁上的每個連結都有一個名為 href 的屬性。
HTML 中連結的標識
現在讓我們討論一下如何識別網頁上顯示的超連結的錨標籤 - 下圖所示。右鍵單擊網頁,然後單擊 Chrome 瀏覽器中的“檢查”按鈕。之後,整個頁面的相應 HTML 程式碼將可見。要檢查頁面上的“Created”連結,請單擊下面突出顯示的左上箭頭。
單擊並指向“Created”超連結後,其 HTML 程式碼可見。
可以使用 Selenium 中的連結文字定位器來識別連結。將識別具有匹配連結文字值的第一個元素。
語法
Webdriver driver = new ChromeDriver();
driver.findElement(By.linkText("value of link text"));
使用連結文字定位器處理連結
讓我們以上面頁面為例,單擊“Created”連結後,頁面上將顯示文字Link has responded with status 201 and status text Created。
示例
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.concurrent.TimeUnit;
public class HandLinks {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will identify an element
driver.get
("https://tutorialspoint.tw/selenium/practice/links.php");
// identify link with link text locator then click
WebElement l = driver.findElement(By.linkText("Created"));
l.click();
// identify text locator
WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]"));
System.out.println("Text appeared is: " + t.getText());
// Closing browser
driver.quit();
}
}
輸出
Text appeared is: Link has responded with status 201 and status text Created Process finished with exit code 0
在上面的示例中,單擊連結Created後獲得的帶有訊息的文字為Link has responded with status 201 and status text Created。
最後,收到訊息Process finished with exit code 0,表示程式碼已成功執行。
使用部分連結文字定位器處理連結
可以使用 Selenium 中的部分連結文字定位器來識別連結。將識別具有匹配部分連結文字值的第一個元素。
語法
Webdriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("value of partial link text"));
示例
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.concurrent.TimeUnit;
public class HandPartialLinks {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will identify an element
driver.get("https://tutorialspoint.tw/selenium/practice/links.php");
// identify link with partial link text locator then click
WebElement l = driver.findElement(By.partialLinkText("Creat"));
l.click();
// identify text locator
WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]"));
System.out.println("Text appeared is: " + t.getText());
// Closing browser
driver.quit();
}
}
輸出
Text appeared is: Link has responded with status 201 and status text Created
在上面的示例中,單擊連結Created(使用部分連結文字)後獲得的帶有訊息的文字為連結Link has responded with status 201 and status text Created。
使用標籤名定位器處理連結
可以使用 Selenium 中的標籤名定位器來識別連結。將識別具有匹配標籤名值的第一個元素。
在上例中,我們單擊並指向“Created”超連結後,其 HTML 程式碼可見,反映了錨標籤名(稱為“a”並用<>括起來)。
<a href="javascript:void(0);" id="created" onclick="shide('create')">Created</a>
語法
Webdriver driver = new ChromeDriver();
driver.findElement(By.tagName("a”));
讓我們以同一個頁面為例,我們首先計算連結的總數,然後單擊特定連結,例如“No Content”。單擊該連結後,我們將獲得文字Link has responded with status 204 and status text。
示例
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.concurrent.TimeUnit;
import java.util.List;
public class TotalLinks {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will identify an element
driver.get("https://tutorialspoint.tw/selenium/practice/links.php");
// identify link with link text locator then click
WebElement l = driver.findElement(By.linkText("No Content"));
l.click();
// 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 ++){
if( totalLnks.get(j).getText().equalsIgnoreCase("No Content") ) {
totalLnks.get(j).click();
WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]"));
// get the browser title to confirm navigation after click
System.out.println( "Get text after click: " + t.getText());
break ;
}
}
// Closing browser
driver.quit();
}
}
輸出
Total number of links: 42 Get text after click: Link has responded with status 204 and status text No Content
在上面的示例中,我們計算了網頁上鍊接的總數,並在控制檯中收到了訊息 - Total number of links: 42 和單擊後獲得的文字 Get text after click: Link has responded with status 204 and status text No Content。
結論
本教程總結了 Selenium Webdriver 處理連結的全面內容。我們首先介紹瞭如何在 HTML 中識別連結,並透過示例說明了如何使用 Selenium Webdriver 中的連結文字、部分連結文字和標籤名定位器來處理連結。這使您掌握了 Selenium Webdriver 處理連結的深入知識。最好不斷練習所學內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬視野。
