JavascriptExecutor in WebdriverIO

JavaScript is the preferred language inside the browser to interact with HTML dom. This means that a Browser has JavaScript implementation in it and understands the JavaScript commands. You can disable it using browser options in your browser.

The javascript, we use to write the out webdriverio scripts same as the javascript used in the browser.

The first thing to know that the JavaScriptExecutor comes separately and also comes under the WebDriver (not webdriverIO) but both do the same thing. Within the WebDriverIO, javascript executor is bundled and it is named as executeScript.

Javascript executed in browser is same for all selenium technologies because it is dependent on browser not on Program which executes it.

We can do anything and everything a developer does use javascript inside an HTML browser. Below you will find the example.

browser.executeScript("javascript or Jquery")

How to enter a value into the textbox:

browser.executeScript("document.getElementById('email').value='[email protected]'")


How to click a button

browser.executeScript("document.querySelector('#enterimg').click()")


How to refresh a window

browser.executeScript("history.go(0)")


How to get the text of a particular web element

var text = js.executeScript("return document.getElementById('btn2').innerHTML").toString();
console.log("WebElement Text is : "+ text);


How to get the title of the WebPage:

browser.executeScript("return document.title")

How to scroll vertically for certain pixels, below code vertically scrolls 500 pixels towards down.

browser.executeScript("window.scrollBy(0,500)")


How to scroll till the bottom of the web page:

browser.executeScript("window.scrollTo(0, document.body.scrollHeight)")


How to scroll to a particular element:

browser.executeScript("document.querySelector('#countries').scrollIntoView()")


How to navigate back to the page

browser.executeScript("window.history.back()")


How to navigate to the next page:

browser.executeScript("window.history.forward()")


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

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • could u pls post on how jquery is active like jquery.active === 0 
    Reply
  • HarshavardhanReddy I
    https://chercher.tech/webdriverio/xpath-webdriverio is not working. Please resume.
    Reply
  • hhhhhhhh
    gggg
    Reply
  • Reply
  • dfsdfsdf
    Reply
  • Kumar Saurabh
    Reply
  • Reply
  • Reply