JavascriptExecutor:
In selenium JavascriptExecutor is an interface.This is available in org.openqa.selenium.JavascriptExecutor and provides mechanism to execute Javascript through selenium driver.
It provides “executescript” to run JavaScript in current frame or window.
We can use JavascriptExecutor to perform actions such as mouse-over, click and get title of browser etc.. using selenium Webdriver.
Actions:
Through Actions special keyboard and mouse events are done such as moveToElement(toElement), keyUp(modifier _key) and doubleClick() etc..The bellow example demonstrates both JavascriptExecutor and Actions
Sample Code:
package com.p1;import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseHoverByJavaScript {
public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C://Selenium//geckodriver-v0.18.0-win64//geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.get("https://www.ebay.com/");
driver.manage().window().maximize();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Object for the MenuItem 'Electronics'
WebElement ele=driver.findElement(By.xpath(".//*[@id='s0-container']/li[6]/a"));
//Object for JavascriptExecutor
JavascriptExecutor jse = (JavascriptExecutor) driver;
try {
//Highlight the menu item 'Electronics' in green color using the JavaScript
jse.executeScript("arguments[0].style.border='4px groove green'", ele);
//Perform Mouse-Over on menu item 'Electronics' using javascript
String mouseOverScript = "if(document.createEvent)"
+ "{"
+ " var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);"
+ "} "
+ "else if(document.createEventObject) "
+ "{ "
+ " arguments[0].fireEvent('onmouseover');"
+ "}";
jse.executeScript(mouseOverScript, ele);
//Click the menu item 'Electronics' using the JavaScript
jse.executeScript("arguments[0].click();", ele);
}
catch (Exception e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
//Navigating to the Home page
driver.findElement(By.xpath(".//*[@id='bc']/li[1]/a")).click();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
//Object for the MenuItem 'Motors'
WebElement mot=driver.findElement(By.xpath(".//*[@id='s0-container']/li[4]/a"));
//Highlight the menu item 'Motors' in red color
jse.executeScript("arguments[0].style.border='4px groove red'", mot);
try {
//Perform Mouse-Over on menu item 'Motors' using Actions
Actions action = new Actions(driver);
action.moveToElement(mot).build().perform();
}
catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.close();
}
}