Sometime in testing, we need to do some operations with mouse and keyboard.
Selenium web driver directly can not handle mouse and keyboard event like double click, right click, pressing key combination in keyboard.
We can handle such type of event with Actions class in selenium webdriver. We have to create object of that class and perform action which we want.
Double Click
Syntax : action.doubleClick(WebElement).build().perform();
ContextClick (Right Click)
Syntax : action.contextClick(WebElement).build().perform();
ClickAndHold
Syntax : action.clickAndHold(WebElement);
KeyDown and KeyUP
Syntax : action.keyDown(Key_modifier) and action.keyUp(Key_modifier)
Key Modifier: Keys.SHIFT, Keys.CONTROL, Keys.ALT etc.
If we want to press keyboard key on particular web element then we also need to pass that element with function.
action.keyDown(WebElement, Key_modifier)
action.keyUP(WebElement, Key_modifier)
We can also do series of multiple action with Actions class. Please visit below blog for how to do series of multiple action with Actions class.
Series of Multiple actions with Selenium
Selenium web driver directly can not handle mouse and keyboard event like double click, right click, pressing key combination in keyboard.
We can handle such type of event with Actions class in selenium webdriver. We have to create object of that class and perform action which we want.
Double Click
Syntax : action.doubleClick(WebElement).build().perform();
Actions action = new Actions(driver); // It is google search text box WebElement wSearch = driver.findElement(By.id("lst-ib")); // enter 'test' on search text box and double click on that. So test will select. wSearch.sendKeys("test"); action.doubleClick(wSearch).build().perform(); Thread.sleep(2000);
ContextClick (Right Click)
Syntax : action.contextClick(WebElement).build().perform();
Actions action = new Actions(driver); // It is google search text box WebElement wSearch = driver.findElement(By.id("lst-ib")); // right click on google search test box action.contextClick(wSearch).build().perform(); Thread.sleep(2000);
ClickAndHold
Syntax : action.clickAndHold(WebElement);
Actions action = new Actions(driver); // It is google search text box WebElement wSearch = driver.findElement(By.id("lst-ib")); // Click and hold on search text box. wSearch.sendKeys("test"); action.clickAndHold(wSearch).build().perform(); Thread.sleep(2000);
KeyDown and KeyUP
Syntax : action.keyDown(Key_modifier) and action.keyUp(Key_modifier)
Key Modifier: Keys.SHIFT, Keys.CONTROL, Keys.ALT etc.
If we want to press keyboard key on particular web element then we also need to pass that element with function.
action.keyDown(WebElement, Key_modifier)
action.keyUP(WebElement, Key_modifier)
Actions action = new Actions(driver); // It is google Search text box WebElement wSearch = driver.findElement(By.id("lst-ib")); // Enter 'Selenium' in capital letters in search text box action.keyDown(wSearch, Keys.SHIFT).sendKeys("selenium").keyUp(wSearch, Keys.SHIFT).build().perform(); Thread.sleep(2000);
We can also do series of multiple action with Actions class. Please visit below blog for how to do series of multiple action with Actions class.
Series of Multiple actions with Selenium