• Selenium Video Tutorials

Selenium 和 JavaScript 教程



Selenium Webdriver 可用於執行 JavaScript 命令來與網頁瀏覽器中出現的元素的 html 互動。這是使用 JavaScriptExecutor 介面實現的。JavaScript 命令與 Selenium Webdriver 一起使用的最常見示例之一是在網頁上執行滾動操作時。

執行 JavaScript 命令的方法

Selenium JavaScriptExecutor 介面中,有多種方法可用於執行 JavaScript 命令。

語法

import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(script, args);

Selenium 中執行 JavaScript 命令的示例

讓我們以以下頁面為例,我們將使用 URL 啟動應用程式:Selenium 自動化實踐表單。然後我們將獲取文字:學生登錄檔單,以及域名 www.tutorialspoint.com。接下來,我們將在“姓名:”標籤旁邊的輸入框中輸入文字JavaScript

Selenium Javascript Tutorial 1

然後,我們將單擊“登入”按鈕,之後我們將導航到另一個頁面,該頁面顯示文字歡迎,登入。最後,我們將重新整理瀏覽器並再次獲取文字、URL 和域名,分別為歡迎,登入Selenium 自動化實踐表單www.tutorialspoint.com

Selenium Javascript Tutorial 2

程式碼實現

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.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class JavaScriptSelenium {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();
      
      // 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("Getting the current URL: " + url);

      //identify text
      WebElement t = driver.findElement(By.xpath("//*[@id='practiceForm']/h1"));
      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("Getting the current domain: " + domain);

      // enter text in input box
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
      j.executeScript("arguments[0].value='JavaScript';", 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: " + 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("Getting 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: " + text3);

      // getting current domain after browser refresh
      String domain1 = j.executeScript("return document.domain;").toString();
      System.out.println("Getting the current domain after browser refresh: " + domain1);

      // Quitting browser
      driver.quit();
   }
}

輸出

Getting the current URL:
 https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php
Text is: Student Registration Form
Getting the current domain: www.tutorialspoint.com
Entered text is: JavaScript
Text found after clicking: Welcome, Login In
Getting the current URL after browser refresh: 
https://tutorialspoint.tw/selenium/practice/login.php
Text found after refresh: Welcome, Login In
Getting the current domain after browser refresh:
 www.tutorialspoint.com

Process finished with exit code 0

在上面的示例中,我們啟動了一個 URL 並獲取了當前 URL,並在控制檯中顯示訊息 - 獲取當前 URL:Selenium 自動化實踐表單。然後分別檢索了文字和域名,並在控制檯中顯示訊息 - 文字為:學生登錄檔單獲取當前域名:www.tutorialspoint.com

接下來,我們在輸入框中輸入了文字 Selenium 並檢索了其值,並在控制檯中顯示訊息 - 輸入的文字為:Selenium。然後,我們單擊登入連結並在導航後獲取文字,並在控制檯中顯示訊息:單擊後找到的文字:複選框

最後,我們重新整理了頁面並獲取了頁面的當前 URL、文字和域名,並在控制檯中顯示訊息 - 瀏覽器重新整理後獲取當前 URL:Selenium 自動化實踐表單重新整理後找到的文字:複選框瀏覽器重新整理後獲取當前域名:www.tutorialspoint.com

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

在本教程中,我們討論瞭如何使用 Selenium Webdriver 使用 JavaScript。

廣告