- 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 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 基於關鍵字的框架
- Selenium - 資料驅動的框架
- Selenium - 混合驅動的框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 瀏覽器導航
Selenium Webdriver 提供多種方法來實現瀏覽器導航,包括啟動、後退、前進瀏覽器歷史記錄以及重新整理瀏覽器。
基本的瀏覽器導航命令
下面討論一些瀏覽器導航方法:
driver.navigate().to("應用程式 URL")
此方法導航到應用程式 URL。
語法
driver.navigate().to("https://tutorialspoint.tw/index.htm");
driver.get("應用程式 URL")
此方法啟動瀏覽器以開啟應用程式。
語法
driver.get("https://tutorialspoint.tw/index.htm");
driver.navigate().back()
此方法根據瀏覽器歷史記錄上下文跳轉到上一頁。
語法
driver.navigate().back();
driver.navigate().forward()
此方法根據瀏覽器歷史記錄上下文跳轉到下一頁。
語法
driver.navigate().forward();
driver.navigate().refresh()
此方法重新載入網頁。
語法
driver.navigate().refresh();
示例 1
讓我們來看一個例子,我們首先啟動一個瀏覽器標題為Selenium Practice - Student Registration Form的應用程式。
然後,我們將點選登入連結,點選後,我們將導航到另一個瀏覽器標題為Selenium Practice - Login的頁面。
接下來,我們將返回瀏覽器歷史記錄,並將瀏覽器標題獲取為Selenium Practice - Student Registration Form。
最後,我們將前進到瀏覽器歷史記錄,並將瀏覽器標題獲取為Selenium Practice - Login。我們還將重新整理瀏覽器,並將瀏覽器標題獲取為Selenium Practice - Login。
程式碼實現
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 BrowNavigation {
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);
// launching a browser and open a URL
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Getting browser title after launch
System.out.println("Getting browser title after launch: " + driver.getTitle());
// identify the link then click
WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
l.click();
// Getting browser title after clicking link
System.out.println("Getting browser title after clicking link: " + driver.getTitle());
// navigate back to browser history
driver.navigate().back();
// Getting browser title after navigating back
System.out.println("Getting browser title after navigating back: " + driver.getTitle());
// navigate forward to browser history
driver.navigate().forward();
// Getting browser title after navigating forward
System.out.println("Getting browser title after navigating forward: " + driver.getTitle());
// refresh browser
driver.navigate().refresh();
// Getting browser title after browser refresh
System.out.println("Getting browser title after browser refresh: " + driver.getTitle());
// Closing browser
driver.quit();
}
}
輸出
Getting browser title after launch: Selenium Practice - Student Registration Form Getting browser title after clicking link: Selenium Practice - Login Getting browser title after navigating back: Selenium Practice - Student Registration Form Getting browser title after navigating forward: Selenium Practice - Login Getting browser title after browser refresh: Selenium Practice - Login
在上面的示例中,我們在開啟的瀏覽器中啟動了一個 URL,並在控制檯中獲得了瀏覽器標題訊息 - 啟動後獲取瀏覽器標題:Selenium Practice - Student Registration Form。然後我們點選了登入連結,並收到了導航頁面的瀏覽器標題,控制檯訊息為 - 點選連結後獲取瀏覽器標題:Selenium Practice - Login。
接下來,我們在瀏覽器歷史記錄中後退,並獲得了瀏覽器標題,控制檯訊息為 - 導航後退後獲取瀏覽器標題:Selenium Practice - Student Registration Form。再次,我們在瀏覽器歷史記錄中前進,並檢索了瀏覽器標題,控制檯訊息為 - 導航前進後獲取瀏覽器標題:Selenium Practice - Login。最後,我們重新整理了瀏覽器並獲得了它的標題,控制檯訊息為 - 瀏覽器重新整理後獲取瀏覽器標題:Selenium Practice - Login。
示例 2
讓我們來看一個例子,我們首先啟動一個 URL 為 - 的應用程式:
https://tutorialspoint.tw/selenium/practice/login.php.
然後,我們將點選註冊連結,點選後,我們將導航到另一個 URL 為 - 的頁面:
https://tutorialspoint.tw/selenium/practice/register.php.
接下來,我們將返回瀏覽器歷史記錄,並將 URL 獲取為 -
https://tutorialspoint.tw/selenium/practice/login.php.
最後,我們將前進到瀏覽器歷史記錄,並將 URL 獲取為 -
https://tutorialspoint.tw/selenium/practice/register.php.
我們還將重新整理瀏覽器,重新整理後我們將獲得 URL 為
https://tutorialspoint.tw/selenium/practice/register.php.
程式碼實現
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 BrowNavigationTo {
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);
// launching a browser and navigate to a URL
driver.navigate().to("https://tutorialspoint.tw/selenium/practice/login.php");
// Getting browser title after launch
System.out.println("Getting current URL after launch: " + driver.getCurrentUrl());
// identify a link then click
WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[3]/a"));
l.click();
// Getting browser title after clicking link
System.out.println("Getting current URL after clicking link: " + driver.getCurrentUrl());
// navigate back to browser history
driver.navigate().back();
// Getting browser title after navigating back
System.out.println ("Getting current URL after navigating back: " + driver.getCurrentUrl());
// navigate forward to browser history
driver.navigate().forward();
// Getting browser title after navigating forward
System.out.println("Getting current URL after navigating forward: " + driver.getCurrentUrl());
// refresh browser
driver.navigate().refresh();
// Getting browser title after browser refresh
System.out.println("Getting current URL after browser refresh: " + driver.getCurrentUrl());
// Closing browser
driver.quit();
}
}
輸出
Getting current URL after launch: https://tutorialspoint.tw/selenium/practice/login.php Getting current URL after clicking link: https://tutorialspoint.tw/selenium/practice/register.php Getting current URL after navigating back: https://tutorialspoint.tw/selenium/practice/login.php Getting current URL after navigating forward: https://tutorialspoint.tw/selenium/practice/register.php Getting current URL after browser refresh: https://tutorialspoint.tw/selenium/practice/register.php
在上面的示例中,我們在開啟的瀏覽器中啟動了一個 URL,並在控制檯中獲得了當前 URL 訊息 - 啟動後獲取當前 URL: https://tutorialspoint.tw/selenium/practice/login.php。
然後我們點選了註冊連結,並在控制檯中收到了導航後的當前 URL 訊息 - 點選連結後獲取當前 URL: https://tutorialspoint.tw/selenium/practice/register.php。
接下來,我們在瀏覽器歷史記錄中後退,並在控制檯中獲得了導航後的當前 URL 訊息 - 導航後退後獲取當前 URL: https://tutorialspoint.tw/selenium/practice/login.php。
再次,我們在瀏覽器歷史記錄中前進,並在控制檯中收到了導航後的當前 URL 訊息 - 導航前進後獲取當前 URL: https://tutorialspoint.tw/selenium/practice/register.php。
最後,我們重新整理了瀏覽器,並在控制檯中收到了當前 URL 訊息 - 瀏覽器重新整理後獲取當前 URL: https://tutorialspoint.tw/selenium/practice/register.php。
因此,在本教程中,我們討論瞭如何使用 Selenium Webdriver 執行瀏覽器導航。
