JavaScriptExecutor in Selenium WebDriver With Exampl

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

JavaScriptExecutor in Selenium

WebDriver with Example


Explanation:

Used above mentioned code under following activities:

 Sending elements without sendkeys


 Alert handling
 Clicking on button
 Scrolling a page
 Scraping of domain name, url information from website

Sample code :

package project1;

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 class1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");

// Instantiate a ChromeDriver class.

WebDriver driver=new ChromeDriver();


//Maximize the browser

driver.manage().window().maximize();

driver.get("https://www.gmail.com");

// WebElement loginButton = driver.findElement(By.xpath("//*[@id='next']"));

/*Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(Script,Arguments);

script - The JavaScript to execute

Arguments - The arguments to the script.(Optional)*/

JavascriptExecutor js = (JavascriptExecutor)driver;

//1..to type text in Selenium WebDriver without using sendKeys() method

//driver.findElement(By.id("identifierId")).sendKeys("jeevi");

//js.executeScript("")

js.executeScript("document.getElementById('identifierId').value='SoftwareTestingMaterial.com';");

//2..to click a button in Selenium WebDriver using JavaScript

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

//or

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

js.executeScript("document.getElementById('next').click();");
//3..to handle checkbox

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

//4..to generate Alert Pop window in selenium

js.executeScript("alert('hello world');");

//5..to refresh browser window using Javascript

js.executeScript("history.go(0)");

//6.to get the Title of our webpage

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

System.out.println(Title);

//7..to get the domain

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

System.out.println(domain);

//8..to get the URL of our webpage

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

System.out.println(URL);

//9..to perform Scroll on application using Selenium

//Vertical scroll - down by 50 pixels

js.executeScript("window.scrollBy(0,50)");

// 10..for scrolling till the bottom of the page we can use the code like

js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
// 11..to click on a SubMenu which is only visible on mouse hover on Menu?

//Hover on Automation Menu on the MenuBar

js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");

//12..to navigate to different page using Javascript?

//Navigate to new Page

js.executeScript("window.location = 'https://www.softwaretestingmaterial.com");

You might also like