- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- 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 提供了多個命令,這些命令有助於開啟瀏覽器,在開啟的瀏覽器上執行某些操作,以及退出瀏覽器。
Selenium Webdriver 中的基本瀏覽器命令
下面討論了一些瀏覽器命令:
get(String URL) 命令
此命令開啟瀏覽器,然後啟動作為引數傳遞的 URL。
語法
driver.get("https://tutorialspoint.tw/selenium/practice/nestedframes.php");
getTitle() 命令
此命令獲取作為字串的焦點瀏覽器頁面標題。它不接受任何引數。
語法
String str = driver.getTitle();
getCurrentUrl() 命令
此命令獲取作為字串的焦點瀏覽器 URL。它不接受任何引數。
語法
String str = driver.getCurrentUrl();
getPageSource() 命令
此命令獲取作為字串的焦點瀏覽器頁面原始碼。它不接受任何引數。
語法
String str = driver.getPageSource();
close() 命令
此命令僅關閉焦點瀏覽器視窗。它不接受任何引數,也不返回任何內容。
語法
driver.close();
quit() 命令
此命令關閉與驅動程式會話相關的所有內容(瀏覽器和所有後臺驅動程式程序)。它不接受任何引數,也不返回任何內容。
語法
driver.quit();
如何在網頁上檢查元素?
首先右鍵單擊網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。接下來將開啟完整頁面的 HTML 程式碼。要調查頁面上的特定元素,請單擊可見 HTML 程式碼頂部左側的向上箭頭,如下所示。
示例 1
讓我們以以下頁面為例,我們首先啟動一個具有 URL 的應用程式:Selenium 自動化練習表單,然後獲取瀏覽器標題Selenium Practice - 學生登錄檔單。
請注意,我們可以在 HTML 程式碼中的title標籤內獲取網頁的瀏覽器標題,該標籤位於head標籤內。在下圖中,我們看到頁面的標題是Selenium Practice - 學生登錄檔單。
然後,我們將單擊登入按鈕,之後我們將導航到另一個頁面,該頁面的瀏覽器標題為Selenium Practice - 登入,URL 為:Selenium 自動化練習表單。最後,我們將退出 Web 驅動程式會話。
程式碼實現
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 BrowserCommand {
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());
// Getting browser URL after launch
System.out.println("Getting URL after launch: " + driver.getCurrentUrl());
// identify 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());
// Getting browser URL after launch
System.out.println("Getting URL after clicking link: " + driver.getCurrentUrl());
// Quitting browser
driver.quit();
}
}
輸出
Getting browser title after launch: Selenium Practice - Student Registration Form Getting URL after launch: https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php Getting browser title after clicking link: Selenium Practice - Login Getting URL after clicking link: https://tutorialspoint.tw/selenium/practice/login.php Process finished with exit code 0
在上面的示例中,我們在開啟的瀏覽器中啟動了一個 URL,並分別獲取了瀏覽器標題和當前 URL,並在控制檯中顯示訊息 - 啟動後獲取瀏覽器標題:Selenium Practice - 學生登錄檔單和啟動後獲取 URL:Selenium 自動化練習表單。
然後我們單擊“登入”,並在導航後接收瀏覽器標題和當前 URL,並在控制檯中顯示訊息 - 單擊連結後獲取瀏覽器標題:Selenium Practice - 登入和單擊連結後獲取 URL:Selenium 自動化練習表單。接下來,我們必須退出驅動程式會話。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
示例 2
讓我們再舉一個例子,如下圖所示,我們將單擊新標籤頁。
之後,我們將獲得另一個包含文字新標籤頁的視窗。
然後我們將關閉包含文字新標籤頁的新視窗,並返回到原始視窗並在那裡訪問文字 - 瀏覽器視窗。最後,我們將退出會話。
程式碼實現
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.Set;
import java.util.concurrent.TimeUnit;
public class WindowClose {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new window
driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");
// click button and navigate to next window
WebElement b = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
b.click();
// Get the window handle of the original window
String oW = driver.getWindowHandle();
// get all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Iterating through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switching to child window
driver.switchTo().window(w);
// accessing element in new window
WebElement e = driver.findElement
(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new window is: " + e.getText());
// closing new window
driver.close();
break;
}
}
// switching to parent window
driver.switchTo().window(oW);
// accessing element in parent window
WebElement e1 = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text in parent window is: " + e1.getText());
// quitting the browser session
driver.quit();
}
}
輸出
Text in new window is: New Tab Text in parent window is: Browser Windows
在上面的示例中,我們捕獲了新視窗上的文字,並在控制檯中收到訊息 - 新視窗中的文字為:新標籤頁。然後我們關閉了子視窗並切換回父視窗。最後,在控制檯中獲取父視窗上的文字 - 父視窗中的文字為:瀏覽器視窗。
示例 3
讓我們再舉一個以上頁面的例子,我們將使用getPageSource()方法獲取頁面原始碼。
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class WindowPagSource {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new window
driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");
// Getting browser page source
System.out.println("Getting page source: " + driver.getPageSource());
// quitting the browser session
driver.quit();
}
}
在上面的示例中,我們捕獲了在網頁上啟動的網頁的頁面原始碼,並在控制檯中顯示訊息。
結論
這結束了我們對 Selenium WebDriver 瀏覽器命令教程的全面介紹。我們首先描述了 Selenium Webdriver 中的基本瀏覽器命令,如何在網頁上檢查元素,並逐步介紹瞭如何使用 Selenium Webdriver 使用瀏覽器命令的示例。這使您對 Selenium WebDriver 瀏覽器命令有了深入的瞭解。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。
