0% found this document useful (0 votes)
18 views3 pages

Javascript

The document describes various ways to perform actions in Selenium using JavaScriptExecutor such as clicking buttons and checkboxes, typing in text boxes, handling alerts, refreshing the page, getting page details like title and URL, scrolling, and finding hidden elements. JavaScriptExecutor allows executing JavaScript code in the browser to perform actions without using the regular Selenium locator strategies. Some key actions covered include clicking elements, typing text, checking checkboxes, getting page details, handling alerts, refreshing, and scrolling.

Uploaded by

lohitj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Javascript

The document describes various ways to perform actions in Selenium using JavaScriptExecutor such as clicking buttons and checkboxes, typing in text boxes, handling alerts, refreshing the page, getting page details like title and URL, scrolling, and finding hidden elements. JavaScriptExecutor allows executing JavaScript code in the browser to perform actions without using the regular Selenium locator strategies. Some key actions covered include clicking elements, typing text, checking checkboxes, getting page details, handling alerts, refreshing, and scrolling.

Uploaded by

lohitj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Step 1: Import the package associated with JavascriptExecutor.

1. To Click on a Button

js.executeScript("document.getElementById('enter element id').click();");

//or
js.executeScript("arguments[0].click();", okButton);

js.executeScript("document.getElementById('enter element id').click();");

//or
js.executeScript("arguments[0].click();", okButton);
-----------------------------------------------------------------------------------
-------------

2. To Type Text in a Text Box without using sendKeys() method

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';");

-----------------------------------------------------------------------------------
-----------------

3. To Handle Checkbox by passing the value as true or false

js.executeScript("document.getElementById('enter element id').checked=false;");


1
js.executeScript("document.getElementById('enter element id').checked=false;");
-----------------------------------------------------------------------------------
----------------
4. To generate Alert Pop window in Selenium Webdriver

js.executeScript("alert('Welcome To Selenium Testing');");


1

js.executeScript("alert('Welcome To Selenium Testing');");


5. To refresh browser window using Javascript

js.executeScript("history.go(0)");
1
js.executeScript("history.go(0)");
6. To get the innertext of the entire webpage in Selenium

String innerText = js.executeScript(" return


document.documentElement.innerText;").toString();
System.out.println(innerText);
1
2
String innerText = js.executeScript(" return
document.documentElement.innerText;").toString();
System.out.println(innerText);
7. To get the Title of our webpage

String titleText = js.executeScript("return document.title;").toString();


System.out.println(titleText);
1
2
String titleText = js.executeScript("return document.title;").toString();
System.out.println(titleText);
8. To get the domain name

String domainName= js.executeScript("return document.domain;").toString();


System.out.println(domainName);
1
2
String domainName= js.executeScript("return document.domain;").toString();
System.out.println(domainName);
9. To get the URL of a webpage

String url= js.executeScript("return document.URL;").toString();


System.out.println(url);
1
2
String url= js.executeScript("return document.URL;").toString();
System.out.println(url);
10. To get the Height and Width of a web page

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

a) To scroll the page vertically for 500px

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);");

You might also like