Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
May 1, 2017

JavaScriptExecutor in Selenium





JavaScriptExecutor is one of the interfaces in selenium. The basic advantage of JavaScriptExecutor is it provides a way to execute JavaScript in Selenium Webdriver. Sometimes locators may not work, in that case JavaScriptExecutor will helps to interact with the web elements on the particular webpage.  The reason behind this is; even selenium webdriver internally converts language bindings into its equivalent JavaScript and injects into the respective browser. JavaScriptExecutor is also very useful to identify and interact with the hidden and disabled elements on the webpage.

As JavaScriptExecutor is an interface so we can not create an object to this, for this we will type caste to driver object using below syntax:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(javaScript, arguments);

Now we will see some of the examples below that how we can use JavaScriptExecutor to execute the JavaScript to interact with the web elements.

1. How to enter value into textbox:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('email').value='krishna@gmail.com'");

2. How to click a button:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('#enterimg').click()");

3. How to refresh a window:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("history.go(0)");

4. How to get the text of a particular web element:

JavascriptExecutor js = (JavascriptExecutor)driver;
String text = js.executeScript("return document.getElementById('btn2').innerHTML").toString();
System.out.println("WebElement Text is : "+ text);





5. How to get the title of the WebPage:

JavascriptExecutor js = (JavascriptExecutor)driver;
String text = js.executeScript("return document.title").toString();
System.out.println("Page Title is : "+ text);

6. How to scroll vertically for certain pixels:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,500)");

The above code vertically scrolls 500 pixels towards down.

7. How to scroll till the bottom of the web page:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

8. How to scroll to a particular element:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('#countries').scrollIntoView()");





9. How to navigate back to page:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.history.back()");

10. How to navigate to next page:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.history.forward()");

This way we can JavaScriptExecutor to perform certain actions on a webpage if the normal selenium webdriver methods are not working.

Please watch YouTube video for better understanding.



Share this post: on Twitter on Facebook on Google+

Read data from Excel to DataProvider in Selenium Handling Textboxes in Selenium

Related Posts

TAKING WEB ELEMENT SCREENSHOT IN SELENIUM

Selenium - Java

How to Capture WebElement Screenshot in Selenium Webdriver using selenium 4

avoid Switch to window

Selenium - Java

How to Avoid Switch To Window in Selenium WebDriver

webdrivermanager

Selenium - Java

No More Driver EXE files for Selenium Execution – Instead Use WebDriverManager

HANDLE TEXTBOXES IN SELENIUM

Selenium - Java

Handling Textboxes in Selenium

ELEMENT NOT VISIBLE EXCEPTION

Selenium - C#, Selenium - Java

Selenium ElementNotVisibleException

NOSUCHWINDOW EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchWindowException

NOSUCHFRAME EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchFrameException

NOSUCHELEMENT EXCEPTION

Selenium - C#, Selenium - Java

Selenium NoSuchElementException

SELENIUM WEBDRIVER EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium WebDriver Exceptions

HANDLING BASIC ELEMENTS IN SELENIUM

Selenium - C#, Selenium - Java

Handling Basic Elements in Selenium

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023