Selenium中的JavascriptExecutor

2023年 8月 28日 15.9k 0

Selenium是一个众所周知的开源、基于Web的自动化工具,被许多人使用。但是有时候它在与某些元素进行交互时会遇到问题;也许一个意外的弹出窗口会阻碍Web驱动程序执行操作并生成错误的结果。这就是JavascriptExecutor在这种情况下发挥关键作用的地方,它使Web驱动程序能够成功执行所需的操作。它的复杂性和突发性并存,使得处理这种情况变得更加容易。

What is JavascriptExecutor in Selenium?

使用名为JavascriptExecutor的接口,可以通过Selenium执行JavaScript,并在使用这种编程语言时与浏览器中的HTML进行交互,必须使用JavascriptExecutor对象,创建长度和复杂度各异的句子结构对于构成引人入胜的文本至关重要。因此,JavaScript Executor提供了与Web浏览器内的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 flexibility. Breaking down the code into easily identifiable components with varying complexity and 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 is run in an efficient and optimized manner.

学习如何使用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:UsersADMINDocumentsSeleniummsedgedriver.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 higher degree of flexibility and customization added to the equation web automation can be 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 advancing their internet persona.

以上就是Selenium中的JavascriptExecutor的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论