Selenium Webdriver with JavaScript (JavaScriptExecutor)

Author - Manpreet Singh
1.08.2018
|
0 Comments
||

How to Use Selenium Webdriver with JavaScript?

Selenium is an open source automation testing tool. It is used to test the web application. Selenium supports multiple languages to create an automation script such as Java, Python, PHP, C#, Ruby.

In case we want to test with Javscript we use JavaScriptExecutor. It provides the mechanism to execute Javascript through Selenium webdriver. It provides “execute script” & “executeAsyncScript” methods to run JavaScript in the selected browser window. To execute JavaScript in webdriver script we do not have to write the separate code.

JavascriptExecutor is a predefined Interface which is available in a package. we just need to import the package in a script to use the Javascript in selenium webdriver.

This Below package is used to execute the javascript With selenium webdriver:

org.openqa.selenium.JavascriptExecutor;

Some of the Operation that we can perform using JavaScriptExecutor in Selenium Webdriver:

1. Type text in textfield in Selenium WebDriver without using sendKeys() method:

Syntax:

openjs.executeScript("document.getElementById('some id').value='someValue';");

Example:

JavascriptExecutor openjs = (JavascriptExecutor)driver;
openjs.executeScript("document.getElementById('Email').value='manpreet.singh@webners.com';");

2. Click on a button in selenium webdriver using JavaScript:

Syntax:

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

Example:

JavascriptExecutor openjs = (JavascriptExecutor)driver;
openjs.executeScript("arguments[0].click();", Sign In);

3. Get the Title of current page in selenium webdriver using JavaScript:

Syntax:

openjs.executeScript"return document.title;");

Example:

JavascriptExecutor openjs = (JavascriptExecutor)driver;
String title =  openjs.executeScript("return document.title;").toString();
System.out.println(title);

4. To find hidden element in selenium using JavaScript:

Syntax:

openjs.executeScript("arguments[0].click();", element);

Example:

 WebElement element = driver.findElement(By.partialLinkText("Gmail"));
 openjs.executeScript("arguments[0].click();", element);

Example of selenium webdriver with javaScript to click on hidden element on page and get title of that page.

steps:

1. Access the url “https://webnersolutions.com”.
2. Open the sub heading using the javascript
3. Get the title of sub heading pages.
4. Exit.

Code:

package Testing;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SJavascript 
{ 
public static void main(String[] args){
WebDriver driver;

//OPEN THE GOOGLE CHROME BROWSER
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
		
//GOTO THE URL
driver.get("https://webnersolutions.com");

// DECLARE THE JAVASCRIPT METHOD
JavascriptExecutor openjs  = (JavascriptExecutor)driver;
		
// CLICK ON THE HIDDEN SUB HEADING ON THE HOME PAGE USING JAVASCRIPT
WebElement element = driver.findElement(By.xpath("//ul[@id = 'menu-main-menu']/li[@id = 'menu-item-7935']/ul/li/a"));
openjs.executeScript("arguments[0].click();", element);
		
//GET THE TITLE OF CURRENT OPEN PAGE
String title =  openjs.executeScript("return document.title;").toString();
System.out.println(title);	
driver.quit();
}
}

RESULT:
Selenium Webdriver

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *