0% found this document useful (0 votes)
149 views4 pages

JavaScriptExecutor in Selenium WebDriver With Exampl

This document discusses using JavaScriptExecutor in Selenium WebDriver. It provides 12 examples of tasks that can be performed using JavaScriptExecutor, including: sending values without using sendKeys; handling alerts; clicking buttons; scrolling pages; and scraping domain names and URLs. It also includes sample code to demonstrate how to instantiate a JavaScriptExecutor and execute scripts to perform these tasks.

Uploaded by

jeevi jeeva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
149 views4 pages

JavaScriptExecutor in Selenium WebDriver With Exampl

This document discusses using JavaScriptExecutor in Selenium WebDriver. It provides 12 examples of tasks that can be performed using JavaScriptExecutor, including: sending values without using sendKeys; handling alerts; clicking buttons; scrolling pages; and scraping domain names and URLs. It also includes sample code to demonstrate how to instantiate a JavaScriptExecutor and execute scripts to perform these tasks.

Uploaded by

jeevi jeeva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 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