JavaScriptExecutor in selenium:
What is JavaScript and why do we use it in selenium?
- Java Script is an object-oriented programming language commonly used to create interactive effects within the web browser to Handle WebElements within Web Page.
- It is used by Selenium WebDriver to do handle WebElements.
- We use Java in our test case and we apply almost all the features but in some features we can not use Java, so let us copy scripting the wanted.
What is JavaScriptExecutor?
- The JavaScriptExecutor is an interface that provides mechanisms to execute javascript through the Selenium Driver. To run JavaScript in the context of the currently selected frame or window, it provides “executescript” and “executeAsyncScript” methods.
- The basic advantage of JavaScriptExecutor is that Selenium provides a way to run Java Script in webdriver.
- Sometimes the locator can not work, in that case the JavaScriptExecutor will help interact with web elements on a particular webpage.
- The reason behind this is; Even Selenium webdriver convert the bindings of the language internally to the equivalent of javascript and inject them into the related browser.
- Javascript extractor is also very useful for identifying hidden elements and interacting with them on the webpage.
Package to import:
1 | import org.openqa.selenium.JavascriptExecutor; |
Syntax:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(ScriptToExecute,OptionalArguments); |
Example of JavaScriptExecutor:
// How to generate Alert Pop window in selenium ?
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("alert('hello world!');"); |
// How to get innertext of the entire webpage in Selenium?
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; String sText = js.executeScript("return document.documentElement.innerText;").toString(); |
// How to navigate to different page using Javascript?
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.location = 'https://www.stqatools.com/'"); |
// How to enter value into textbox:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; |
// How to click a button:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.querySelector('#btn').click()"); |
// How to refresh a window:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("history.go(0)"); |
// How to get the text of a particular web element:
1 2 3 4 5 | JavascriptExecutor js = (JavascriptExecutor) driver; String text = js.executeScript("return document.getElementById('btn').innerHTML").toString(); System.out.println("WebElement Text is : " + text); |
// How to get the title of the WebPage:
1 2 3 4 5 | JavascriptExecutor js = (JavascriptExecutor) driver; String title = js.executeScript("return document.title").toString(); System.out.println("Page Title is : " + text); |
// How to scroll vertically for certain pixels:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,500)"); |
// How to scroll till the bottom of the web page:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); |
// How to scroll to a particular element:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.querySelector('#countries').scrollIntoView()"); |
// How to navigate back to page:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.history.back()"); |
// How to navigate to forward page:
1 2 3 | JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.history.forward()"); |
Example of JavaScriptExecutor in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecutor { public static void main(String[] args) throws IOException { // Path of geckodriver System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // How to generate Alert Pop window in selenium ? JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("alert('hello world!');"); // How to get innertext of the entire webpage in Selenium? String sText = js.executeScript("return document.documentElement.innerText;").toString(); // How to navigate to different page using Javascript? js.executeScript("window.location = 'https://www.stqatools.com'"); // How to enter value into textbox: // How to click a button: js.executeScript("document.querySelector('#enterimg').click()"); // How to refresh a window: js.executeScript("history.go(0)"); // How to get the text of a particular web element: String text = js.executeScript("return document.getElementById('btn2').innerHTML").toString(); System.out.println("WebElement Text is : " + text); // How to get the title of the WebPage: String title = js.executeScript("return document.title").toString(); System.out.println("Page Title is : " + text); // How to scroll vertically for certain pixels: js.executeScript("window.scrollBy(0,500)"); // How to scroll till the bottom of the web page: js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); // How to scroll to a particular element: js.executeScript("document.querySelector('#countries').scrollIntoView()"); // How to navigate back to page: js.executeScript("window.history.back()"); // How to navigate to forward page: js.executeScript("window.history.forward()"); } } |
JavaScriptExecutor in Selenium WebDriver With Examples and JavaScript and Selenium JavaScriptExecutor and Selenium Javascriptexecutor Tutorial and How to use JavaScriptExecutor in selenium WebDriver