首页 > Java > java教程 > 正文

Selenium中的JavascriptExecutor

王林
发布: 2023-08-19 19:21:09
转载
1046 人浏览过

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.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!