
- 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 - 處理IFrames
- 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 - JavaScript 執行器
Selenium WebDriver 用於執行 JavaScript 命令來與頁面上瀏覽器中顯示的元素進行通訊。這是藉助 JavaScriptExecutor 介面實現的。有時,Selenium WebDriver 中可用的定位器無法與頁面上的元素互動,在這種情況下,我們可以藉助 JavaScriptExecutor 方法。
執行 JavaScript 命令的基本方法
JavaScriptExecutor 介面中有多種可用方法。它們列在下面:
executeScript 方法
此方法用於在當前活動的視窗或框架中執行 JavaScript。它是一個匿名函式,支援各種資料型別,例如 WebElement、Long、String、List 和 Boolean。
executeAsyncScript 方法
這是一種多執行緒方法,用於在當前活動的視窗或框架中執行單個 JavaScript 任務。它允許頁面解析繼續進行,從而最佳化效能並提供更大的靈活性。此方法能夠以最佳方式執行非同步 JavaScript。
執行 JavaScript 命令的步驟
使用 JavaScriptExecutor 介面結合 Selenium WebDriver 執行 JavaScript 命令的步驟列在下面:
步驟 1 - 匯入 JavaScriptExecutor 的包。
步驟 2 - 建立 JavaScriptExecutor 介面的引用變數。
步驟 3 - 呼叫 JavaScriptExecutor 方法。
語法
import org.openqa.selenium.JavascriptExecutor; JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript(script, args);
示例
讓我們以以下頁面為例,我們將在其中開啟一個帶有 URL 的應用程式:Selenium 自動化實踐表單。
然後,獲取文字:Selenium - 自動化實踐表單,以及域為www.tutorialspoint.com。接下來,我們將在名稱標籤旁邊的輸入框中輸入文字Selenium。

然後,我們將單擊登入,因此我們將導航到一個新頁面,該頁面包含文字歡迎,登入中。我們將重新整理瀏覽器,然後獲取文字、URL 和域,分別為歡迎,登入中、Selenium 自動化實踐表單和www.tutorialspoint.com。

程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class JavaScriptsExe { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 25 secs driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS); // Creating a reference variable JavaScriptExecutor interface. JavascriptExecutor j = (JavascriptExecutor) driver; // launching a URL j.executeScript("window.location = 'https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php'"); // getting current URL String url = j.executeScript("return document.URL;").toString(); System.out.println("Get the current URL: " + url); //identify text WebElement t = driver.findElement(By.xpath("/html/body/div/header/div[2]/h1")); // get text String text = (String) j.executeScript("return arguments[0].innerText", t); System.out.println("Text is: " + text); // getting current domain String domain = j.executeScript("return document.domain;").toString(); System.out.println("Get the current domain: " + domain); // enter text in input box WebElement e = driver.findElement(By.xpath("//*[@id='name']")); j.executeScript("arguments[0].value='Selenium';", e); // get text entered String text1 = (String) j.executeScript("return arguments[0].value", e); System.out.println("Entered text is: " + text1); // perform click WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a")); j.executeScript("arguments[0].click();", b); //identify text WebElement w = driver.findElement(By.xpath("//*[@id='signInForm']/h1")); // get text after click String text2 = (String) j.executeScript("return arguments[0].innerText", w); System.out.println("Text found after clicking is: " + text2); // refresh browser j.executeScript("history.go(0)"); // getting current URL after browser refresh String url1 = j.executeScript("return document.URL;").toString(); System.out.println("Get the current URL after browser refresh: " + url1); //identify text again after refresh WebElement y = driver.findElement(By.xpath("//*[@id='signInForm']/h1")); // get text after refresh String text3 = (String) j.executeScript("return arguments[0].innerText", y); System.out.println("Text found after refresh is: " + text3); // getting current domain after browser refresh String domain1 = j.executeScript("return document.domain;").toString(); System.out.println("Get the current domain after browser refresh: " + domain1); // Quit browser driver.quit(); } }
輸出
Get the current URL: https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php Text is: Selenium - Automation Practice Form Get the current domain: www.tutorialspoint.com Entered text is: Selenium Text found after clicking is: Welcome, Login In Get the current URL after browser refresh: https://tutorialspoint.tw/selenium/practice/login.php Text found after refresh is: Welcome, Login In Get the current domain after browser refresh: www.tutorialspoint.com Process finished with exit code 0
在上面的示例中,我們啟動了一個 URL 並獲取了當前 URL,控制檯中的訊息為:獲取當前 URL:Selenium 自動化實踐表單
然後檢索文字和域,控制檯中的訊息分別為:文字為:Selenium - 自動化實踐表單和獲取當前域:www.tutorialspoint.com。
接下來,我們在輸入框中輸入文字 Selenium 並檢索其值,控制檯中的訊息為:輸入的文字為:Selenium。然後,我們單擊“登入”連結,並在導航後獲取文字,控制檯中的訊息為:單擊後找到的文字:歡迎,登入中。最後,我們重新整理了頁面並獲取了頁面的當前 URL、文字和域,控制檯中的訊息為:瀏覽器重新整理後獲取當前 URL:Selenium 有用資源
重新整理後找到的文字:歡迎,登入中,以及瀏覽器重新整理後獲取當前域:www.tutorialspoint.com。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
示例 - 使用 JavaScriptExecutor 向下滾動
現在讓我們討論如何使用 scrollTo 方法向下滾動到網頁底部,並獲取該頁面底部顯示的按鈕登入,如下圖所示。scrollTo 方法採用兩個引數 - 水平和垂直頂點。

程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class PageDownsScroll { 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 perform the scroll driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // JavascriptExecutor to scrolling to page bottom JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript("window.scrollBy(0,document.body.scrollHeight)"); // access element at page bottom after scrolling WebElement w = driver.findElement(By.xpath("//*[@id='practiceForm']/div[11]/input")); System.out.println("Verify element presence after scroll down: " + w.isDisplayed()); // quit the browser driver.quit(); } }
輸出
Verify element presence after scroll down: true
要獲取有關使用 Selenium WebDriver 執行滾動操作的更多資訊,請參考連結Selenium WebDriver 滾動操作。
示例 - 使用 JavaScriptExecutor 建立警報
讓我們以以下頁面為例,我們將在其中使用 JavaScriptExecutor 在網頁上建立警報並獲取文字Tutorialspoint。

程式碼實現
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class JavaScriptAlert { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Creating a reference variable JavaScriptExecutor interface JavascriptExecutor j = (JavascriptExecutor) driver; // create an Alert j.executeScript("alert('Tutorialspoint!!!');"); // switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); // Quit browser driver.quit(); } }
輸出
Alert text is: Tutorialspoint!!!
在上面的示例中,我們建立了一個警報並獲取了它的文字,控制檯中的訊息為:警報文字為:Tutorialspoint!!!
示例 - 使用 JavaScriptExecutor 選擇複選框
讓我們再舉一個以下頁面的例子,我們將使用 JavaScript Executor 選中複選框以選擇主級別 2選項:

程式碼實現
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class JavaScriptHandlesCheckbox { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Creating a reference variable JavaScriptExecutor interface JavascriptExecutor j = (JavascriptExecutor) driver; // Opening the webpage where we will check checkbox driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php"); // identify checkbox with xpath WebElement chkbox = driver.findElement(By.xpath("//*[@id='c_bs_2']")); // check the checkbox j.executeScript("document.getElementById('c_bs_2').checked=true;"); // check if checkbox is selected System.out.println("Checkbox is selected: " + chkbox.isSelected()); // Quitting browser driver.quit(); } }
輸出
Checkbox is selected: true
在上面的示例中,我們選中了一個複選框並驗證了它是否被選中,控制檯中的訊息為:複選框已選中:true
結論
這總結了我們關於 Selenium WebDriver JavaScript 執行器教程的全面內容。我們從描述執行 JavaScript 命令的基本方法、執行 JavaScript 命令的步驟以及示例來說明如何在 Selenium WebDriver 中處理 JavaScript 執行器開始。這使您能夠深入瞭解 Selenium WebDriver JavaScript 執行器。明智的做法是繼續練習您所學的內容並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。