Javascript
Javascript
1. To Click on a Button
//or
js.executeScript("arguments[0].click();", okButton);
//or
js.executeScript("arguments[0].click();", okButton);
-----------------------------------------------------------------------------------
-------------
js.executeScript("document.getElementById(id').value='someValue';");
js.executeScript("document.getElementById('Email').value='SeleniumTesting.com';");
1
2
js.executeScript("document.getElementById(id').value='someValue';");
js.executeScript("document.getElementById('Email').value='SeleniumTesting.com';");
-----------------------------------------------------------------------------------
-----------------
js.executeScript("history.go(0)");
1
js.executeScript("history.go(0)");
6. To get the innertext of the entire webpage in Selenium
js.executeScript(“return window.innerHeight;”).toString();
js.executeScript(“return window.innerWidth;”).toString();
1
2
js.executeScript(“return window.innerHeight;”).toString();
js.executeScript(“return window.innerWidth;”).toString();
11. To find a hidden element in selenium using JavaScriptExecutor
js.executeScript("arguments[0].click();", element);
1
js.executeScript("arguments[0].click();", element);
12. To navigate to a different page using Javascript
js.executeScript("window.location = 'https://www.lambdatest.com");
1
js.executeScript("window.location = 'https://www.lambdatest.com");
13. To perform Scroll on an application using Selenium
js.executeScript(“window.scrollBy(0,500)”);
1
js.executeScript(“window.scrollBy(0,500)”);
b) To scroll the page vertically till the end
js.executeScript(“window.scrollBy(0,document.body.scrollHeight)”);
1
js.executeScript(“window.scrollBy(0,document.body.scrollHeight)”);
14. Adding an element in DOM
We can also add an element in DOM if required. However, as are only concerned with
mimicking user interactions in the browser, this option may not be used.
js.executeScript("var btn=document.createElement('newButton');"
+ "document.body.appendChild(btn);");
1
2
3
js.executeScript("var btn=document.createElement('newButton');"
+ "document.body.appendChild(btn);");