This post was most recently updated on June 14th, 2019
In Selenium Action class is used to perform
- drag and drop operation
- move any element
- Press composite key
- hover on element
Click action related function with their uses
Click and Move Actions | Their uses |
click() | Perform left click action on current mouse position. |
click(WebElement target) | Perform left click action on center of given web element. |
contextClick() | Perform right/context click action on current mouse position. |
contextClick(WebElement target) | Perform right/context click action on center of given web element. |
doubleClick() | Perform double left click action on current mouse position. |
doubleClick(WebElement target) | Perform double left click action on center of given web element. |
clickAndHold() | Perform left click action on current mouse position and left mouse button not released. |
clickAndHold(WebElement target) | Perform left click action on center of given web element and left mouse button not released. |
moveByOffset(int xOffset, int yOffset) | Move mouse cursor from current location to given location. |
moveToElement(WebElement target) | Move mouse cursor from current location to mid of targeted element. |
moveToElement(WebElement target, int xOffset, int yOffset) | Move mouse cursor from given element’s top-left corner to given offset location. |
Selenium code to perform click using click(WebElement target) function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.youidraw.com/apps/painter/"); //create object of Actions class Actions builder=new Actions(driver); //Create object of brush (button web element) WebElement brush=driver.findElement(By.id("brush")); //create web element for canvas WebElement canvas=driver.findElement(By.xpath("//canvas[@id='catch']")); //Used click(WebElement) //build Action object by adding sequence of actions Action clickOnBrush=builder.click(brush).build(); //perform action clickOnBrush.perform(); //create new action object Action clickOnCanvas=builder.click(canvas).build(); //perform action clickOnCanvas.perform(); //commented to see result //driver.quit(); } } |
Selenium code to perform click using moveToElement(WebElement target) and click() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.youidraw.com/apps/painter/"); //create object of Actions class Actions builder=new Actions(driver); //Create object of brush (button web element) WebElement brush=driver.findElement(By.id("brush")); //create web element for canvas WebElement canvas=driver.findElement(By.xpath("//canvas[@id='catch']")); //To use click() function we have to use moveToElement before click //build Action object by adding sequence of actions Action moveAndClickOnBrush=builder.moveToElement(brush).click().build(); //perform action moveAndClickOnBrush.perform(); Action moveAndClickOnCanvas=builder.moveToElement(canvas).click().build(); //perform action moveAndClickOnCanvas.perform();//verify click action mid of canvas //commented to see result //driver.quit(); } } |
Use of contextClick() and contextClick(WebElement target)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.youidraw.com/apps/painter/"); //create object of Actions class Actions builder=new Actions(driver); //Create object of brush (button web element) WebElement brush=driver.findElement(By.id("brush")); //create web element for canvas WebElement canvas=driver.findElement(By.xpath("//canvas[@id='catch']")); //To use click() function we have to use moveToElement before click //build Action object by adding sequence of actions Action moveToAndClickOnBrush=builder.moveToElement(brush).click().build(); //perform action moveToAndClickOnBrush.perform(); Action moveToAndClickOnCanvas=builder.moveToElement(canvas).click().build(); //perform action moveToAndClickOnCanvas.perform();//verify click action mid of canvas //contextClick Action rightClickOnCanvas=builder.contextClick(canvas).build(); // Select All shown by system after performing context click rightClickOnCanvas.perform(); //OR //contextClick(WebElement target) Action rightClickOnCanvas2Mtd=builder.moveToElement(canvas).contextClick().build(); // Select All shown by system after performing context click rightClickOnCanvas2Mtd.perform(); //below(commented) code to select context menu option //WebElement selectAllContextMenu=driver.findElement(By.xpath("//ul[@id='menuContext']/li[@style='display: list-item;' or not(@style)]/a[.='Select All']")); //Action selectMenuOption=builder.click(selectAllContextMenu).build(); //selectMenuOption.perform(); //commented to see result //driver.quit(); } } |
Use of moveToElement(WebElement target, int xOffset, int yOffset) and release()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.youidraw.com/apps/painter/"); //create object of Actions class Actions builder=new Actions(driver); //Create web element for shape WebElement shape=driver.findElement(By.id("shape")); //create web element for canvas WebElement canvas=driver.findElement(By.xpath("//canvas[@id='catch']")); //click on shape web element Action moveAndClickOnShape=builder.moveToElement(shape).click().build(); //perform action moveAndClickOnShape.perform(); int startPointX=canvas.getLocation().getX(); int startPointY=canvas.getLocation().getY(); //using moveByOffset, clickAndHold and release function Action drawSquare=builder.moveByOffset(startPointX+10, startPointY+10) .clickAndHold() .moveToElement(canvas) .release().build(); drawSquare.perform(); //OR //using moveByElement(WebElement, offsetX, offsetY), clickAndHold and release function Action drawSquare2=builder.moveToElement(canvas, 10, 10) .clickAndHold() .moveToElement(canvas) .release().build(); drawSquare2.perform(); //commented to see result //driver.quit(); } } |