Selenium是一个众所周知的开源、基于Web的自动化工具,被许多人使用。但是有时候它在与某些元素进行交互时会遇到问题;也许一个意外的弹出窗口会阻碍Web驱动程序执行操作并生成错误的结果。这就是JavascriptExecutor在这种情况下发挥关键作用的地方,它使Web驱动程序能够成功执行所需的操作。它的复杂性和突发性并存,使得处理这种情况变得更加容易。
使用名为JavascriptExecutor的接口,可以通过Selenium执行JavaScript,并在使用这种编程语言时与浏览器中的HTML进行交互,必须使用JavascriptExecutor对象,创建长度和复杂度各异的句子结构对于构成引人入胜的文本至关重要。因此,JavaScript Executor提供了与Web浏览器内的HTML进行通信的手段,同时还使程序员能够使用自己独特的JavaScript编写风格来构建巧妙灵活的表达。
以下是Selenium中JavascriptExecutor提供的方法:
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.
第一步 - 导入包
import org.openqa.selenium.JavascriptExecutor;
第二步 - 创建一个引用
javascriptExecutor js = (JavascriptExecutor) driver;
第三步 - 调用JavascriptExecutor方法
js.executeScript(script, args);
// importing the package Import org.openqa.selenium.JavascriptExecutor; // creating a reference JavascriptExecutor js = (JavascriptExecutor) driver; // calling the method js.executeScript(script, args);
刷新浏览器窗口。
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("location.reload()");
To send the text.
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementByID(‘element id ’).value = ‘xyz’;");
生成警告弹窗。
JavascriptExecutor js = (JavascriptExecutor)driver; Js.executeScript("alert(‘hello world’);");
To get the Inner text of a web page.
avascriptExecutor js = (JavascriptExecutor)driver; string sText = js.executeScript("return document.documentElement.innerText;").toString();
To get the title of the web page.
avascriptExecutor js = (JavascriptExecutor)driver; string sText = js.executeScript("return document.title;").toString();
滚动页面。
JavascriptExecutor js = (JavascriptExecutor)driver; //Vertical scroll – down by 150 pixels js.executeScript("window.scrollBy(0,150)");
在这个例子中,我们使用selenium web driver和javascriptExecutor来打开WaytoClass网站并点击一个元素。
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);"
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); } }
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.
注意 - 在显示上述输出后,它将自动打开网站并点击元素。
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中文网其他相关文章!