In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key etc...) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.
In order to perform action events, we need to use org.openqa.selenium.interactions.Actions class.
sample :
// Configure the Action
Actions myaction = new Actions(driver);
// To click on the element
myaction.moveToElement(element).click().perform();
Go to Site : www.flipcart.com and search for iPhone and press down arrow two times and then hit enter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActionsKeys { | |
public static void main(String[] args) throws InterruptedException { | |
// TODO Auto-generated method stub | |
WebDriver driver = new FirefoxDriver(); | |
driver.get("http://www.flipcart.com/"); | |
Thread.sleep(5000); | |
// to enter text in search filed. | |
WebElement txtSearch=driver.findElement(By.xpath("//*[@id='fk-top-search-box']")); | |
txtSearch.sendKeys("Iphone"); | |
Thread.sleep(2000); | |
//Create an object for Actions class | |
Actions a = new Actions(driver); | |
//Simulate Enter key | |
//Simulate Page Down | |
a.sendKeys(Keys.ARROW_DOWN).perform(); | |
a.sendKeys(Keys.ARROW_DOWN).perform(); | |
//Simulate page Up | |
a.sendKeys(Keys.ENTER).perform(); | |
Thread.sleep(2000); | |
} | |
} |