JavaScriptExecutor in Selenium WebDriver With Exampl
JavaScriptExecutor in Selenium WebDriver With Exampl
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;
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver.manage().window().maximize();
driver.get("https://www.gmail.com");
/*Syntax:
js.executeScript(Script,Arguments);
JavascriptExecutor js = (JavascriptExecutor)driver;
//driver.findElement(By.id("identifierId")).sendKeys("jeevi");
//js.executeScript("")
js.executeScript("document.getElementById('identifierId').value='SoftwareTestingMaterial.com';");
//js.executeScript("arguments[0].click();", loginButton);
//or
js.executeScript("document.getElementById('next').click();");
//3..to handle checkbox
js.executeScript("alert('hello world');");
js.executeScript("history.go(0)");
System.out.println(Title);
System.out.println(domain);
System.out.println(URL);
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?
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
js.executeScript("window.location = 'https://www.softwaretestingmaterial.com");