It is used to handle operation using keyboard events such as key-up,key-down, and mouse events such as mouse-over,mouse-enter,mouse-out, etc.
Selenium Web Driver provides an Action class to manage these operations.
Selenium Web Driver provides an Action class to manage these operations.
Actions ref = new Actions(driver);
Action ref1 = ref.moveToElement('elementname').build()
ref1.perform()
Example of Keyboard and MouseEvent:-
package contect_us;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class ActionExample {
public static void main(String[] args) {
System.setProperty("Webdriver.chromedriver", "c://chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement ele = driver.findElement(By.name("email"));
Actions ref = new Actions(driver);
Action ac = ref.moveToElement(ele).click().keyDown(ele,Keys.SHIFT).sendKeys(ele,"abcd").keyUp(ele,Keys.SHIFT).doubleClick(ele).contextClick().build();
ac.perform();
}
}
Action ref1 = ref.moveToElement('elementname').build()
ref1.perform()
Example of Keyboard and MouseEvent:-
package contect_us;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class ActionExample {
public static void main(String[] args) {
System.setProperty("Webdriver.chromedriver", "c://chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement ele = driver.findElement(By.name("email"));
Actions ref = new Actions(driver);
Action ac = ref.moveToElement(ele).click().keyDown(ele,Keys.SHIFT).sendKeys(ele,"abcd").keyUp(ele,Keys.SHIFT).doubleClick(ele).contextClick().build();
ac.perform();
}
}
Another Example of Keyboard and Mouse Based Events:-
package scs;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class KeyBoardAndMosueEventExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://eroomrent.in/index.php");
WebElement ele = driver.findElement(By.linkText("Owner-G"));
Actions ack= new Actions(driver);
Action event = ack.moveToElement(ele).click().build();
event.perform();
WebElement ele1 = driver.findElement(By.partialLinkText("Login"));
ele1.click();
}
}
POST Answer of Questions and ASK to Doubt