Join us on :

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.

First thing to know that the JavaScriptExecutor comes separately and also comes under the WebDriver (not webdriverIO) but both do the same thing. With in 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 an developer does using javascript inside html browser. Below you will find the example.


									browser.executeScript("javascript or Jquery")
									



How to enter value into textbox:


									browser.executeScript("document.getElementById('email').value='abc@gmail.com'")
									


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 page


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


How to navigate to 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.

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions