Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Selenium Essentials

You're reading from  Selenium Essentials

Product type Book
Published in Mar 2015
Publisher
ISBN-13 9781784394332
Pages 194 pages
Edition 1st Edition
Languages
Author (1):
Prashanth Sams Prashanth Sams
Author Profile Icon Prashanth Sams
Prashanth Sams
Toc

The JavascriptExecutor class


JavascriptExecutor is a class under the Selenium library that executes JavaScript code snippets. For example, a Selenium WebDriver command such as WebElement.click() might not work on all browsers, but JavaScriptExecutor could help you to click on an element in any browser by executing the appropriate JavaScript snippet. There are a couple of Selenium WebDriver functions to handle JavaScript, such as executeAsyncScript() and executeScript(). The executeAsyncScript() method lets you inject JavaScript snippets into the page for asynchronous execution. The following is the syntax for this method:

JavascriptExecutor jse = (JavascriptExecutor)driver;

Let's see how you can click on an element without using Selenium's click() method. The following code snippet displays an alert message as a pop-up:

(JavascriptExecutor) driver).executeScript("arguments[0].click();", WebElement element);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("alert('Selenium...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime