搜索
首页Javajava教程Selenium中的JavascriptExecutor

Selenium中的JavascriptExecutor

Aug 19, 2023 pm 07:21 PM
selenium关键词javascriptexecutor

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:\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 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的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
为什么Java是开发跨平台桌面应用程序的流行选择?为什么Java是开发跨平台桌面应用程序的流行选择?Apr 25, 2025 am 12:23 AM

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

讨论可能需要在Java中编写平台特定代码的情况。讨论可能需要在Java中编写平台特定代码的情况。Apr 25, 2025 am 12:22 AM

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

与平台独立性相关的Java开发的未来趋势是什么?与平台独立性相关的Java开发的未来趋势是什么?Apr 25, 2025 am 12:12 AM

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强键入如何有助于平台独立性?Java的强键入如何有助于平台独立性?Apr 25, 2025 am 12:11 AM

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

说明Java本机界面(JNI)如何损害平台独立性。说明Java本机界面(JNI)如何损害平台独立性。Apr 25, 2025 am 12:07 AM

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

是否有任何威胁或增强Java平台独立性的新兴技术?是否有任何威胁或增强Java平台独立性的新兴技术?Apr 24, 2025 am 12:11 AM

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

JVM的实现是什么,它们都提供了相同的平台独立性?JVM的实现是什么,它们都提供了相同的平台独立性?Apr 24, 2025 am 12:10 AM

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性如何降低发展成本和时间?平台独立性如何降低发展成本和时间?Apr 24, 2025 am 12:08 AM

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能