Best Selenium code snippet using org.openqa.selenium.interactions.Interface Action
Source:MouseActions.java
1package org.fluentlenium.core.action;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.interactions.Coordinates;4import org.openqa.selenium.interactions.HasInputDevices;5import org.openqa.selenium.interactions.Mouse;6/**7 * Execute actions with the mouse.8 */9public class MouseActions {10 private final WebDriver driver;11 /**12 * Creates a new mouse actions.13 *14 * @param driver driver15 */16 public MouseActions(WebDriver driver) {17 this.driver = driver;18 }19 /**20 * Get the actions object.21 *22 * @return actions object23 */24 protected org.openqa.selenium.interactions.Actions actions() {25 return new org.openqa.selenium.interactions.Actions(driver);26 }27 /**28 * Basic mouse operations29 *30 * @return low level interface to control the mouse31 * @deprecated Use the following mapping for updating your code:32 * <p>33 * {@link Mouse#click(Coordinates)} to {@link MouseElementActions#click()}34 * <p>35 * {@link Mouse#doubleClick(Coordinates)} to {@link MouseElementActions#doubleClick()}36 * <p>37 * {@link Mouse#mouseDown(Coordinates)} to {@link MouseElementActions#moveToElement()}38 * then {@link MouseElementActions#clickAndHold()}39 * <p>40 * {@link Mouse#mouseUp(Coordinates)} to {@link MouseElementActions#release()}41 * <p>42 * {@link Mouse#mouseMove(Coordinates)} to {@link MouseElementActions#moveToElement()}43 * <p>44 * {@link Mouse#mouseMove(Coordinates, long, long)} to {@link MouseElementActions#moveToElement(int, int)}45 * <p>46 * {@link Mouse#contextClick(Coordinates)} to {@link MouseElementActions#contextClick()}47 */48 @Deprecated49 public Mouse basic() {50 return ((HasInputDevices) driver).getMouse();51 }52 /**53 * Clicks (without releasing) at the current mouse location.54 *55 * @return this object reference to chain calls56 * @see org.openqa.selenium.interactions.Actions#clickAndHold()57 */58 public MouseActions clickAndHold() {59 actions().clickAndHold().perform();60 return this;61 }62 /**63 * Releases the depressed left mouse button at the current mouse location.64 *65 * @return this object reference to chain calls66 * @see org.openqa.selenium.interactions.Actions#release()67 */68 public MouseActions release() {69 actions().release().perform();70 return this;71 }72 /**73 * Clicks at the current mouse location. Useful when combined with74 *75 * @return this object reference to chain calls76 * @see org.openqa.selenium.interactions.Actions#click()77 */78 public MouseActions click() {79 actions().click().perform();80 return this;81 }82 /**83 * Performs a double-click at the current mouse location.84 *85 * @return this object reference to chain calls86 */87 public MouseActions doubleClick() {88 actions().doubleClick().perform();89 return this;90 }91 /**92 * Performs a context-click at the current mouse location.93 *94 * @return this object reference to chain calls95 * @see org.openqa.selenium.interactions.Actions#contextClick()96 */97 public MouseActions contextClick() {98 actions().contextClick().perform();99 return this;100 }101 /**102 * Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates103 * provided are outside the viewport (the mouse will end up outside the browser window) then104 * the viewport is scrolled to match.105 * @param xOffset horizontal offset. A negative value means moving the mouse left.106 * @param yOffset vertical offset. A negative value means moving the mouse up.107 * @return this object reference to chain calls108 * @see org.openqa.selenium.interactions.Actions#moveByOffset(int, int)109 */110 public MouseActions moveByOffset(int xOffset, int yOffset) {111 actions().moveByOffset(xOffset, yOffset).perform();112 return this;113 }114}...
Source:KeyboardActions.java
1package org.fluentlenium.core.action;2import org.openqa.selenium.Keys;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.interactions.HasInputDevices;6import org.openqa.selenium.interactions.Keyboard;7/**8 * Execute actions with the keyboard.9 */10public class KeyboardActions {11 private final WebDriver driver;12 /**13 * Creates a new object to execute actions with the keyboard, using given selenium driver.14 *15 * @param driver selenium driver16 */17 public KeyboardActions(WebDriver driver) {18 this.driver = driver;19 }20 /**21 * Get selenium interactions actions.22 *23 * @return selenium actions24 */25 protected org.openqa.selenium.interactions.Actions actions() {26 return new org.openqa.selenium.interactions.Actions(driver);27 }28 /**29 * Basic keyboard operations30 *31 * @return low level interface to control the keyboard32 * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}33 * and {@link KeyboardActions#sendKeys(CharSequence...)} instead34 */35 @Deprecated36 public Keyboard basic() {37 return ((HasInputDevices) driver).getKeyboard();38 }39 /**40 * Performs a modifier key press. Does not release the modifier key - subsequent interactions41 * may assume it's kept pressed.42 * Note that the modifier key is <b>never</b> released implicitly - either43 * <i>keyUp(theKey)</i> or <i>sendKeys(Keys.NULL)</i>44 * must be called to release the modifier.45 *46 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the47 * provided key is none of those, {@link IllegalArgumentException} is thrown.48 * @return this object reference to chain calls49 * @see org.openqa.selenium.interactions.Actions#keyDown(CharSequence)50 */51 public KeyboardActions keyDown(Keys theKey) {52 actions().keyDown(theKey).perform();53 return this;54 }55 /**56 * Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined57 * behaviour.58 *59 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.60 * @return this object reference to chain calls61 * @see org.openqa.selenium.interactions.Actions#keyUp(CharSequence)62 */63 public KeyboardActions keyUp(Keys theKey) {64 actions().keyUp(theKey).perform();65 return this;66 }67 /**68 * Sends keys to the active element. This differs from calling69 * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:70 * <ul>71 * <li>The modifier keys included in this call are not released.</li>72 * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching73 * elements should work. </li>74 * </ul>75 *76 * @param keysToSend The keys.77 * @return A self reference.78 * @see org.openqa.selenium.interactions.Actions#sendKeys(CharSequence...)79 */80 public KeyboardActions sendKeys(CharSequence... keysToSend) {81 actions().sendKeys(keysToSend).perform();82 return this;83 }84}...
Source:Demo2.java
1package p4_jul31;23import java.io.IOException;4import java.util.List;5import java.util.Scanner;67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.interactions.Action;12import org.openqa.selenium.interactions.Actions;13import org.openqa.selenium.support.ui.Select;1415/*we can use Select class only on the listbox (<select>) else we get UnexpectedTagNameException16 * Action is interface , Actions is a class17 * present in interactions package18 * new ChromeDriver();-----no arg con19 * new Actions(WebDriver);- 1arg con-WebDriver20 * new Select(WebElement); 1arg con-WebElement21 * When we call any method of Actions class we must call perform() at the end 22 */23public class Demo2 {24 static {25 System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");26 System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");27 }28 public static void main(String[] args) throws IOException, InterruptedException {29 WebDriver driver=new ChromeDriver();30 driver.get("D:\\Day11_july31\\ActionDemo.html");31 driver.findElement(By.id("A1")).click();32 Thread.sleep(1000);33 //String xp="(//a[text()='About us '])[2]"; dropdown-toggle34 String xp="(//a[text()='Certification'])[2]";35 WebElement menu = driver.findElement(By.xpath(xp));36 Actions actions=new Actions(driver);37 actions.moveToElement(menu).perform();38 Thread.sleep(1000);39 //String xp2="(//a[text()='Facts & Figures '])[2]";40 String xp3="(//a[text()='Advanced Level '])[2]";41 driver.findElement(By.xpath(xp3)).click();42 //driver.quit();43 44 }4546}
...
Source:Demo6.java
1package p4_jul31;23import java.io.IOException;4import java.util.List;5import java.util.Scanner;67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.interactions.Action;12import org.openqa.selenium.interactions.Actions;13import org.openqa.selenium.support.ui.Select;1415/*we can use Select class only on the listbox (<select>) else we get UnexpectedTagNameException16 * Action is interface , Actions is a class17 * present in interactions package18 * new ChromeDriver();-----no arg con19 * new Actions(WebDriver);- 1arg con-WebDriver20 * new Select(WebElement); 1arg con-WebElement21 * When we call any method of Actions class we must call perform() at the end 22 */23public class Demo6 {24 static {25 System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");26 System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");27 }28 public static void main(String[] args) throws IOException, InterruptedException {29 WebDriver driver=new ChromeDriver();30 driver.get("D:\\Day11_july31\\ActionDemo.html");31 driver.findElement(By.id("A4")).click();32 Thread.sleep(5000);33 String xp1="//h1[text()='Block 1']";34 WebElement block1 = driver.findElement(By.xpath(xp1));35 36 String xp3="//h1[text()='Block 3']";37 WebElement block3 = driver.findElement(By.xpath(xp3));38 39 40 Actions actions=new Actions(driver);41 actions.dragAndDrop(block1, block3).perform();42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 }5758}
...
Source:Demo4.java
1package p4_jul31;23import java.io.IOException;4import java.util.List;5import java.util.Scanner;67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.interactions.Action;12import org.openqa.selenium.interactions.Actions;13import org.openqa.selenium.support.ui.Select;1415/*we can use Select class only on the listbox (<select>) else we get UnexpectedTagNameException16 * Action is interface , Actions is a class17 * present in interactions package18 * new ChromeDriver();-----no arg con19 * new Actions(WebDriver);- 1arg con-WebDriver20 * new Select(WebElement); 1arg con-WebElement21 * When we call any method of Actions class we must call perform() at the end 22 */23public class Demo4 {24 static {25 System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");26 System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");27 }28 public static void main(String[] args) throws IOException, InterruptedException {29 WebDriver driver=new ChromeDriver();30 driver.get("D:\\Day11_july31\\ActionDemo.html");31 driver.findElement(By.id("A2")).click();32 Thread.sleep(1000);33 String xp="//span[text()='right click me']";34 WebElement element = driver.findElement(By.xpath(xp));35 Actions actions=new Actions(driver);36 actions.contextClick(element).perform();37 Thread.sleep(1000);38 driver.findElement(By.xpath("//span[text()='Quit']")).click();39 }4041}
...
Source:InteractionTests.java
1/*2Copyright 2012 Selenium committers3Copyright 2012 Software Freedom Conservancy4Licensed under the Apache License, Version 2.0 (the "License");5you may not use this file except in compliance with the License.6You may obtain a copy of the License at7 http://www.apache.org/licenses/LICENSE-2.08Unless required by applicable law or agreed to in writing, software9distributed under the License is distributed on an "AS IS" BASIS,10WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11See the License for the specific language governing permissions and12limitations under the License.13*/14package org.openqa.selenium.interactions;15import org.junit.runner.RunWith;16import org.junit.runners.Suite;17import org.openqa.selenium.interactions.touch.TouchDoubleTapTest;18import org.openqa.selenium.interactions.touch.TouchFlickTest;19import org.openqa.selenium.interactions.touch.TouchLongPressTest;20import org.openqa.selenium.interactions.touch.TouchScrollTest;21import org.openqa.selenium.interactions.touch.TouchSingleTapTest;22@RunWith(Suite.class)23@Suite.SuiteClasses({24 ActionsTest.class,25 BasicKeyboardInterfaceTest.class,26 BasicMouseInterfaceTest.class,27 CombinedInputActionsTest.class,28 CompositeActionTest.class,29 IndividualKeyboardActionsTest.class,30 IndividualMouseActionsTest.class,31 TouchDoubleTapTest.class,32 TouchFlickTest.class,33 TouchLongPressTest.class,34 TouchScrollTest.class,35 TouchSingleTapTest.class36})37public class InteractionTests {38}...
Source:Demo5.java
1package p4_jul31;23import java.io.IOException;4import java.util.List;5import java.util.Scanner;67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.interactions.Action;12import org.openqa.selenium.interactions.Actions;13import org.openqa.selenium.support.ui.Select;1415/*we can use Select class only on the listbox (<select>) else we get UnexpectedTagNameException16 * Action is interface , Actions is a class17 * present in interactions package18 * new ChromeDriver();-----no arg con19 * new Actions(WebDriver);- 1arg con-WebDriver20 * new Select(WebElement); 1arg con-WebElement21 * When we call any method of Actions class we must call perform() at the end 22 */23public class Demo5 {24 static {25 System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");26 System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");27 }28 public static void main(String[] args) throws IOException, InterruptedException {29 WebDriver driver=new ChromeDriver();30 driver.get("D:\\Day11_july31\\ActionDemo.html");31 driver.findElement(By.id("A3")).click();32 Thread.sleep(1000);33 String xp="//input[@value='Double Click']";34 WebElement button = driver.findElement(By.xpath(xp));35 Actions actions=new Actions(driver);36 actions.doubleClick(button).perform();37 Thread.sleep(2000);38 driver.quit();39 40 }4142}
...
Source:TouchActions.java
1package mouse_Actions;23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.interactions.HasTouchScreen;8import org.openqa.selenium.interactions.Locatable;9import org.openqa.selenium.interactions.TouchScreen;10import org.openqa.selenium.interactions.internal.Coordinates;111213public class TouchActions {1415 public static void main(String[] args) 16 {17 //Keyboard interface class18 WebDriver driver=new ChromeDriver();19 driver.get("https://www.hdfcbank.com/");20 driver.manage().window().maximize();21 22 23 //Enable Touch controls on automation browser24 TouchScreen touch=((HasTouchScreen)driver).getTouch();25 26 //Duplicate element27 WebElement Element=driver.findElement(By.xpath("//input"));28 //Get Coordinate for element29 Coordinates Ele_co=((Locatable)Element).getCoordinates();30 31 32 touch.doubleTap(Ele_co);33 touch.singleTap(Ele_co);34 touch.longPress(Ele_co);35 36 37 /*38 * Tocuh action39 * https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/touch/TouchActions.html40 */41 42 43 44 }4546}
...
Interface Action
Using AI Code Generation
1package selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.interactions.Actions;7public class MouseHover {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 Actions a = new Actions(driver);12 a.moveToElement(sign).build().perform();13 }14}15package selenium;16import org.openqa.selenium.By;17import org.openqa.selenium.WebDriver;18import org.openqa.selenium.WebElement;19import org.openqa.selenium.chrome.ChromeDriver;20import org.openqa.selenium.interactions.Actions;21public class MouseHover {22 public static void main(String[] args) {23 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");24 WebDriver driver = new ChromeDriver();25 Actions a = new Actions(driver);26 a.moveToElement(sign).build().perform();27 }28}29package selenium;30import org.openqa.selenium.By;31import org.openqa.selenium.WebDriver;32import org.openqa.selenium.WebElement;33import org.openqa.selenium.chrome.ChromeDriver;34import org.openqa.selenium.interactions.Actions;35public class MouseHover {36 public static void main(String[] args) {37 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");38 WebDriver driver = new ChromeDriver();39 Actions a = new Actions(driver);
Interface Action
Using AI Code Generation
1package selenium.webdriver;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.interactions.Actions;7public class MouseHover {8 public static void main(String[] args) throws InterruptedException {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\gaurav\\Downloads\\chromedriver_win32\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 Actions action = new Actions(driver);12 action.moveToElement(element).build().perform();13 Thread.sleep(5000);14 driver.close();15 }16}17In this post, we will learn how to perform mouse hover using Selenium WebDriver. We will be using the Actions class of org.openqa.selenium.interactions package to perform the mouse hover operation. The Actions class provides a number of methods to perform mouse and keyboard actions on the web page. We will be using the moveToElement() method of the Actions class to perform the mouse hover operation. We will be using the build() method to build the action and perform() method to perform the action. Let’s see how to perform mouse hover operation using
Interface Action
Using AI Code Generation
1public class MouseHover {2public static void main(String[] args) {3WebDriver driver = new FirefoxDriver();4WebElement element = driver.findElement(By.linkText("Gmail"));5Actions action = new Actions(driver);6action.moveToElement(element).build().perform();7driver.findElement(By.linkText("Sign in")).click();8driver.quit();9}10}
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!