首頁 > Java > java教程 > Selenium中的JavascriptExecutor

Selenium中的JavascriptExecutor

王林
發布: 2023-08-19 19:21:09
轉載
1118 人瀏覽過

Selenium是一個眾所周知的開源、基於Web的自動化工具,被許多人使用。但是有時它在與某些元素進行交互時會遇到問題;也許一個意外的彈出視窗會阻礙Web驅動程式執行操作並產生錯誤的結果。這就是JavascriptExecutor在這種情況下發揮關鍵作用的地方,它使Web驅動程式能夠成功執行所需的操作。它的複雜性和突發性並存,使得處理這種情況變得更加容易。

What is JavascriptExecutor in Selenium?

使用名為JavascriptExecutor的接口,可以透過Selenium執行JavaScript,並在使用這種程式語言時與瀏覽器中的HTML進行交互,必須使用JavascriptExecutor對象,創建長度和復雜度各異的句子結構對於構成引人入勝的文字至關重要。因此,JavaScript Executor提供了與網頁瀏覽器內的HTML進行溝通的手段,同時也使程式設計師能夠使用自己獨特的JavaScript編寫風格來建立巧妙靈活的表達。

Methods

以下是Selenium中JavascriptExecutor提供的方法:

ExecuteScript

的中文翻譯為:

執行腳本

#Executing JavaScript in the presently chosen window or frame has never been so easy! By calling an anonymous function, this method enables users to reap the rewards of a multitude of data types, including −

  • #Web Elements

  • #Lists

  • #Strings

  • #Long

  • 布林值

  • ExecuteAsyncScript

Asynchronous JavaScript execution is a multi-threaded approach to execute individual JavaScript tasks in the current window or frame. It allows page parsing to continue, optimizing performance and providing great flex the gring 組合 gring ableing context is key to achieving this objective. This approach involves creating concise segments in some areas while accommodating lengthier and intricate sections in other parts. With this method, the asynchronous JavaScript 是

學習如何使用JavascriptExecutor

  • 第一步

    - 導入套件

    #
    import org.openqa.selenium.JavascriptExecutor;
    
    登入後複製
  • 第二步

    - 建立一個引用

    #
    javascriptExecutor js = (JavascriptExecutor) driver;
    
    登入後複製
  • 第三步

    - 呼叫JavascriptExecutor方法

    js.executeScript(script, args);
    
    登入後複製
  • Implementation

Example

的中文翻譯為:

範例

// importing the package
Import org.openqa.selenium.JavascriptExecutor;

// creating a reference
JavascriptExecutor js = (JavascriptExecutor) driver;

// calling the method
js.executeScript(script, args);
登入後複製

Examples of JavascriptExecutor in Selenium

Example 1

刷新瀏覽器視窗。

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("location.reload()");
登入後複製

Example 2

To send the text.

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("document.getElementByID(‘element id ’).value = ‘xyz’;");
登入後複製

Example 3

產生警告彈跳窗。

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript("alert(‘hello world’);");
登入後複製

Example 4

To get the Inner text of a web page.

avascriptExecutor js = (JavascriptExecutor)driver;

string sText =  js.executeScript("return document.documentElement.innerText;").toString();
登入後複製

Example 5

To get the title of the web page.

avascriptExecutor js = (JavascriptExecutor)driver;

string sText =  js.executeScript("return document.title;").toString();
登入後複製

Example 6

的中文翻譯為:

範例 6

滾動頁面。

JavascriptExecutor js = (JavascriptExecutor)driver;

 //Vertical scroll – down by 150 pixels

 js.executeScript("window.scrollBy(0,150)");
登入後複製

使用javascriptExecutor選擇一個元素

在這個範例中,我們使用selenium web driver和javascriptExecutor來開啟WaytoClass網站並點擊一個元素。

Explanation

The following mentioned script will launch edge browser, take you to the WaytoClass website, and use javascriptExecutor to click a certain element. So, let’s check how it functions.

    Create an edge driver class and provide the path of youredgedriver.exe in the system property "webdriver.edge.driver".
  • #Maximize the window by using driver.manage().window().maximize()
  • 使用 driver.get("URL 連結") 開啟網址
  • #使用finddby xpath方法取得Java元素"driver.findElement(By.xpath("xpath位址"));"
  • Create a reference for javascriptExecutor by using javascriptExecutor js=(javascriptExecutor) driver;"
  • 呼叫javascriptExecutor方法並傳遞要點擊的網頁元素 "js.executeScript("arguments[0].click();",webelement);"
  • #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;
public class waytoclass {
   public static void main(String args[]) {
      System.setProperty(
         "webdriver.edge.driver",
         "C:\Users\ADMIN\Documents\Selenium\msedgedriver.exe");
	
      // Instantiate a Driver class.
      WebDriver driver = new EdgeDriver();
	
      // Maximize the browser
      driver.manage().window().maximize();
	
      // Launch Website
      driver.get("https://www.waytoclass.com/");
	
      WebElement java = driver.findElement(
         By.xpath("//*[@id="hslider"]/li[6]/a"));
	
      // Create a reference
      JavascriptExecutor js = (JavascriptExecutor)driver;
	
      // Call the JavascriptExecutor methods
      js.executeScript("arguments[0].click();", java);
   }
}
登入後複製

Output

#
Starting MSEdgeDriver 98.0.1108.56 (9a336a18ae89157b3c7ea0568a9cbced8ebc3f7) on port 55401
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping MSEdgeDriver safe. MSEdgeDriver was started successfully.
登入後複製

注意

- 在顯示上述輸出後,它將自動開啟網站並點擊元素。

Selenium中的JavascriptExecutor結論

Enhancing automation capabilities on the web is made possible through the use of JavascriptExecutor allowing developers to engage with page elements beyond what is ordinarily feasible using Selenium. Moreover, with a ​​dwation gretomation of gretomation 完成greatly improved in terms of speed and efficiency. Despite its complexity for inexperienced coders who are not versed in the intricacies of JavaScript, mastering this language can enable organizations which strive towards adnetcing the.

以上是Selenium中的JavascriptExecutor的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板