{"id":3136,"date":"2026-01-09T06:25:38","date_gmt":"2026-01-09T11:25:38","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3136"},"modified":"2026-01-09T06:25:40","modified_gmt":"2026-01-09T11:25:40","slug":"handling-mouse-click-and-keyboard-event-in-selenium-webdriver-using-actions-class","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/handling-mouse-click-and-keyboard-event-in-selenium-webdriver-using-actions-class\/","title":{"rendered":"Handling Mouse Click and Keyboard Event in Selenium Webdriver using Actions Class"},"content":{"rendered":"\n<p>In this article, we will learn to handle Mouse Click &amp; Keyboard Event in Selenium Webdriver.<\/p>\n\n\n\n<p>Using Advanced User Interactions API, we can handle special mouse and keyboard events in <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing<\/a>. These API contains Action classes and Actions that are needed when executing keyboard and mouse events. Action class provides the most commonly used mouse and keyboard events. Selenium has an in-built facility to handle different types of mouse and keyboard events. We use <strong><em>org.openqa.selenium.interactions<\/em><\/strong>&nbsp;Actions class to do action events. We use these selenium actions class rather than using Mouse or Keyboard directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Methods Available in Selenium Actions Class:<\/strong><\/h2>\n\n\n\n<p><strong>Keyboard Events Using Selenium Actions Class API:<\/strong><\/p>\n\n\n\n<p>The Keyboard interface has the methods mentioned below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sendKeys(keysToSend):<\/strong> It sends a series of keystrokes&nbsp;onto the element<\/li>\n\n\n\n<li><strong>keyDown(theKey):<\/strong> It does not release the modifier key. Subsequent interactions may assume it as kept pressed.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<p>Modifier key sample for KeyDown = keys.ALT, keys.SHIFT, or keys.CONTROL, etc.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>keyUp(theKey):<\/strong> Releases already pressed modified key<\/li>\n<\/ul>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<p>Modifier key sample for KeyUp = keys.ALT, keys.SHIFT, or keys.CONTROL, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Mouse Events Using Selenium Actions Class API:<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API-1024x683.png\" alt=\"Mouse Events Using Selenium Actions Class API\" class=\"wp-image-34030\" style=\"aspect-ratio:1.4992888417882142;width:700px;height:auto\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API-1024x683.png 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API-300x200.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API-768x512.png 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API-150x100.png 150w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/05\/Mouse-Events-Using-Selenium-Actions-Class-API.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>The Mouse interface has the methods mentioned below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>click():<\/strong> It simply clicks on the element<\/li>\n\n\n\n<li><strong>clickAndHold():<\/strong> It clicks at the mouse pointed location(without releasing)&nbsp;<\/li>\n\n\n\n<li><strong>contextClick():<\/strong> It performs a context-click at the mouse pointed location (Right Click)<\/li>\n\n\n\n<li><strong>doubleClick():<\/strong> It performs a double-click at the mouse pointed location<\/li>\n\n\n\n<li><strong>dragAndDrop(source, target):<\/strong> It performs a click-and-hold action at the location of the source element, and moves to the location of the target element, then it releases the mouse<\/li>\n<\/ul>\n\n\n\n<p>source- <a href=\"https:\/\/www.h2kinfosys.com\/blog\/webelement-in-selenium\/\">web element to emulate<\/a> button down at.<\/p>\n\n\n\n<p>target- web element to move to and release the mouse at.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>dragAndDrop(source, x-offset, y-offset):<\/strong> It performs click-and-hold at the source element location of the target element and moves by a given offset, and then it releases the mouse.<\/li>\n<\/ul>\n\n\n\n<p>source- web element to emulate button down at.<\/p>\n\n\n\n<p>xOffset- offset of horizontal move<\/p>\n\n\n\n<p>yOffset- offset of vertical move<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>moveByOffset(x-offset, y-offset):<\/strong> It moves the mouse from its current position by the given offset.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>x-Offset- horizontal offset. (negative value = moving the mouse left)<\/p>\n\n\n\n<p>y-Offset- vertical offset. (negative value = moving the mouse up)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>moveToElement(toElement):<\/strong> It moves the mouse to the center of the web element<\/li>\n\n\n\n<li><strong>release():<\/strong> It releases the pressed left mouse button at the current mouse location pointer<\/li>\n<\/ul>\n\n\n\n<p>In the following example, we use the method moveToElement() to mouse-over. Consider the example below:<\/p>\n\n\n\n<p>Go to the <a href=\"https:\/\/amazon.in\" rel=\"nofollow noopener\" target=\"_blank\">amazon.in<\/a> website and Mouse-hover to Account &amp; Lists and click on Your Account.<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"437\" alt=\"amazon.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/cDZQ6wMfr5SLeofqd2CW4LXE6EmkTqjbk7J_hV_rcbcNTLmon0FXcsT9DU26aqy2CviZsBhR_1yyA0E8MZsKO2uMPHgXBJptOwExJY8AZ9bUZpcAW19QIcNxpxa-rwXt3p77_llVBaVV3lmCaA\" title=\"\"><\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"393\" alt=\"334abdff-1a15-416c-8d81-6889c7122ee9.jpg\" src=\"https:\/\/lh3.googleusercontent.com\/VNvm-im-AVpfL-DN2MSahP3sYsE2kI6anEm3wSfBWYGKIm8tz2U0TcnfGp3d6Wgt-7RRhqrCjFR7z4pDpj24Bct4bT036ip6hosfqp7BmINWnSA6ccaeyObHYNkYL5PPZ9gbZmEZMeTwS0x9lA\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Import the Action and Actions classes<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/rRejGtSnjeEKVffzjxw-Bok3me4dXFIppb1XIeQvtO_VTTj13WQBlGwTwOCJbblvqKqcq_1OWjPMzA-nWOVz9B3BJTqDbyqBkM8FG8BfSRa8JIsx82Ur2-JdHmD1G-HnGvD-ysEJpSquQs-DnQ\" alt=\"import1.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 2: <\/strong>Instantiate an Actions class<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/3jtXBru3jh01LpznqmU0D3jaaIhSlp0PpwwZVHBdZcr_VVnskWp86Il9e3mR6dOft2V-IOHY-uduUpZ9PZQVV6KS_p1NXRW8T-hRDBXfUNL1KzSlS_8Bz3CMn2SXwplSkOBxhS2hOND30dGOGg\" alt=\"actions.png\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Instantiate an Action using the Actions object<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Jm4jq73FC_St-D7cmV_rewFA5ptlB3wPQss-yB7zObVDpA4qup7CKERasYIYvGE9WrZ8uxlzCCfDAQLwHZIesLMTOTr22CH5d6BHXz6aKXpzgD8ZEGUVNwBZbht0c7qNQSzE0Rc1ZCJO1A7CCg\" alt=\"actions.png\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4:<\/strong> When executing the Action object, use the perform() method.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/che694q6VN1o1EhAL_cYrvIdRt7Le1OLaQJGIqFuagtzYe6vgTo7l3xiHVhmspyzThsJbLqL-Oj56IoNsKyV8vh5T6BMROqGekyUd_LmIiQtpDn6NpSBkcHgWgS8y43OlVfmHPFopcbdjh29hw\" alt=\"perform.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Below is the whole WebDriver code to mouse-hover to Account &amp; Lists and click on Your Account link.<\/p>\n\n\n\n<p>Let&#8217;s create a test case in which we will automate the following scenarios to handle Mouse-Hover:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke a Google chrome browser.<\/li>\n\n\n\n<li>Open URL: https:\/\/www.amazon.in\/<\/li>\n\n\n\n<li>Mouse-hover to Account &amp; Lists and click on Your Account link.<\/li>\n\n\n\n<li>Close the browser.<\/li>\n<\/ul>\n\n\n\n<p>Now, we will create a test case step by step to understand how to handle Mouse-hover in WebDriver.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch Eclipse IDE.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Go to File &gt; New &gt; Click on Java Project.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/QijVpkdhcLgpVgKiLS4GSmfgD3QWGNeMwJJjCMuLy5YdKnYA3-gcERFAcZ9EyiYnm-JWzOOkvG5qKS86Kdg0IM7O60KmsEhxl50x5Ys0UyCD6N-nIMqUaj2aKMAwTEzWZ9WGVp6QupYFuWV8jg\" alt=\"File.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Right-click on the Project folder and click on the New &gt; class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/JUbMk9dr0ODfxfi2toGNbhUlH-6128PdtWHZafTYE6CE62O_DurLR4DUDCHqdtQjkcVVgqrHvY93peAIHQywvdQNysRy9UQIPvjNX3DSSSJ_0aLryhpmR4NoD4Dn1KhpyxA9p12NSiR4Px_WPw\" alt=\"mouse.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Give your Class name as &#8220;Test_Mousehover&#8221; and Select the checkbox \u201cpublic static void main(String[] args) and click on the &#8220;Finish&#8221; button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/ysN24iHhSoDsXpzdDSEo3IqbiJwWYtMCcOcHu9CSUsBieuJcymMDAWLKXAnkVgBfiSEie-JEhV9JIO_e1oFLnbJ79ztNdvoaygKGpNryj7qkyBZFa1L-tafyGG4c4_6537yKXER99Ay8yue3bg\" alt=\"test.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4:<\/strong> Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.<\/p>\n\n\n\n<p>Here is the sample code to set Chrome driver system property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;\n\n&nbsp;System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");<\/code><\/pre>\n\n\n\n<p>After that, we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\nWebDriver driver=new ChromeDriver();<\/code><\/pre>\n\n\n\n<p>We will get the below code to launch the Google Chrome browser after combining both of the above codes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");&nbsp;&nbsp;\n\nWebDriver driver=new ChromeDriver();<\/code><\/pre>\n\n\n\n<p>&nbsp;&nbsp;After that, we need to navigate to the desired URL.<\/p>\n\n\n\n<p>Below is the sample code to navigate to the desired URL:<\/p>\n\n\n\n<p>\/\/ Launch Website&nbsp;&nbsp;driver.navigate().to(&#8220;https:\/\/www.amazon.in\/&#8221;);<\/p>\n\n\n\n<p>Here is the complete code for above scenario:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.WebDriver;&nbsp;&nbsp;\nimport org.openqa.selenium.chrome.ChromeDriver;&nbsp;&nbsp;\n&nbsp;&nbsp;\npublic class Test_Dropdown {&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;\n&nbsp;System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver=new ChromeDriver();&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Launch Website&nbsp;&nbsp;\ndriver.navigate().to(\"https:\/\/www.amazon.in\/\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;\n}&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Step 5:<\/strong> Now we will try to Mouse-Hover to Accounts &amp; Lists and click on Your Account.<\/pre>\n\n\n\n<p>Follow the below steps to locate the drop-down menu on the web page.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open URL:&nbsp;https:\/\/www.amazon.in\/<\/li>\n\n\n\n<li>Right-click on the Accounts &amp; Lists on the web page and click on Inspect<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/Uh79-GRXqm4nHmPFWFycq9iR_4ehyeYXZoepCWzWcewxyTRqL4s6w93Sk1adJMlF4Ed6GpUDlB0FOs2b3Jb5Qtjgw0e9IWaJiql7tlMT3ooM-yKYVnx7niy8rp0gOIxRKjOAzOi-rK_t_LOVXw\" alt=\"account.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 6:<\/strong> Now we try to locate the \u201cAccounts &amp; Lists\u201d by inspecting its HTML code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/UYBkouuRC5zz2CQADlJFTlc3JMcGnLZmN3JMmTBj7uayhmmN2wy2Zp57QnRlmW9EiE50Ufc5dRsmByQSihmlV0-ZQkdkD5Wm5L8gYBuxfPPFIbLn9fmsW073XLNJlvelzNYrOd2VMIK0LTVx7A\" alt=\"lists.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Note the id attribute of \u201cAccounts &amp; Lists\u201d.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/23UHBgXKFQfFmfVl9GGHuX2B-UWY_cLcvkKbOUwZBA6dXCa6O9V0PjhE88ZE4o9ABjcThoc_oRuCDepIA9x8R8jw0XPHqT1rGJUgBqrU4SnFu9GGnQwpf4dSxoRWMKZF1B6__4J4LQVs1eEc9Q\" alt=\"id.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 7: <\/strong>&nbsp;Now we need to write the code for Account &amp; Lists.<\/p>\n\n\n\n<p>Below is the sample code for Account &amp; Lists:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WebElement menuSignIn = driver.findElement(By.id(\u201cnav-link-accountList\u201d));\n\nActions act = new Actions (driver);\n\nact.moveToElement(menuSignIn).perform();<\/code><\/pre>\n\n\n\n<p><strong>Step 8: <\/strong>&nbsp;Now inspect the element \u201cYour Account\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/NIpYkiW7A9E-VL-3i_eOJ6xITh1R_IZF-vg8J6qxiFhmhzc-nieJ4gOp-_ZNTD6JWeQGNmWgT_Y_h86VzoxQrNUXKjWb5zO4IC4f21LGmraurDPjJ5tJQ6ukpt2bBbWTOaqT8AW7JelCXPoAAw\" alt=\"your.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Now the Xpath of the \u201cYour Account\u201d is<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/U9gs18GKNeDyrGXRfzpD3Kacu5jebldDQjcS0jwpZc8HNx1D6L5iQ32hAAuzK2VVHg-tcvU2P5ho3EoWRmJKO2wgdx-xNZJyMzIukQzmMeywN2C6GMIlYK8mvfObxnsjDSQNbXi1130wRKB50Q\" alt=\"xpath.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 9: <\/strong>&nbsp;Now we need to write the code for Your Account.<\/p>\n\n\n\n<p>Below is the sample code for Your Account&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>driver.findElement(By.xpath(\u201c\/\/span&#91;text()=\u2019Your Account\u2019]\u201d)).click();<\/code><\/pre>\n\n\n\n<p><strong>Now,&nbsp;our&nbsp;final&nbsp;test&nbsp;script&nbsp;will&nbsp;look&nbsp;something&nbsp;like:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> org.openqa.selenium.By;<br><strong>import<\/strong> org.openqa.selenium.WebDriver;<br><strong>import<\/strong> org.openqa.selenium.WebElement;<br><strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;<br><strong>import<\/strong> org.openqa.selenium.interactions.Actions;<br><br><strong>public<\/strong> <strong>class<\/strong> Test_Mousehover {<br><br><strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {<br>\/\/ <strong>TODO<\/strong> Auto-generated method stub<br><br>&nbsp; \/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;<br>System.<em>setProperty<\/em>(\"webdriver.chrome.driver\",\"D:\\\\ChromeDriver\\\\chromedriver.exe\");&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; \/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp; &nbsp; WebDriver driver=<strong>new<\/strong> ChromeDriver();&nbsp;&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Launch Website&nbsp;&nbsp;<br>driver.navigate().to(\"https:\/\/www.amazon.in\/\");&nbsp;&nbsp;&nbsp;&nbsp;<br><br>WebElement menuSignIn = driver.findElement(By.<em>id<\/em>(\"nav-link-accountList\"));<br><br>Actions act = <strong>new<\/strong> Actions (driver);<br>act.moveToElement(menuSignIn).perform();<br><br>driver.findElement(By.<em>xpath<\/em>(\"\/\/span&#91;text()=Your Account']\")).click();&nbsp;&nbsp;<br><br>&nbsp; &nbsp; \/\/ Close the Browser&nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; driver.close();<br><br>&nbsp; &nbsp; }<br><br><br>}<br><br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Keyboard Interface Methods<\/strong><\/h2>\n\n\n\n<p><strong>sendKeys(onElement, charSequence):<\/strong> This sendKeys() method is used to send a series of keys to a web element.<\/p>\n\n\n\n<p><strong>releaseKey():<\/strong> This method releases the key on the keyboard and presskeys<\/p>\n\n\n\n<p><strong>pressKeys():<\/strong> This method is used to post special keys of the keyboard like \u201cshift\u201d, \u201cctrl\u201d, \u201cf1\u201d, \u201ctab\u201d, etc.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> java.util.concurrent.TimeUnit;\n&nbsp;\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.Keys;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.openqa.selenium.interactions.Action;\n<strong>import<\/strong> org.openqa.selenium.interactions.Actions;\n\n<strong>public<\/strong> <strong>class<\/strong> keyboardEvents {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\nSystem.<em>setProperty<\/em>(\"webdriver.chrome.driver\",\"D:\\\\ChromeDriver\\\\chromedriver.exe\");&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; \/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;\n\n&nbsp; &nbsp; WebDriver driver=<strong>new<\/strong> ChromeDriver();&nbsp;&nbsp;\n\n\ndriver.get(\"http:\/\/www.google.com\/\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().timeouts().implicitlyWait(10, TimeUnit.<strong><em>SECONDS<\/em><\/strong>);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebElement text = driver.findElement(By.<em>name<\/em>(\"q\"));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actions make&nbsp; = <strong>new<\/strong> Actions(driver);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action keyboardEvents = make.keyDown(text, Keys.<strong><em>SHIFT<\/em><\/strong>).sendKeys(\"Selenium Webdriver Java\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.keyUp(text, Keys.<strong><em>SHIFT<\/em><\/strong>).doubleClick().contextClick().build();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keyboardEvents.perform();\n}\n\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">From the above example, The keyword \u201cSelenium Webdriver Java\u201d is sent to the google site search box. Thus, it is changed to the UPPERCASE with method keyDown() and the method doubleClick() double clicks on it to highlight the text and method contextClick() double click on the text and thus contextual menu is displayed.\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/6py3897kpATLn7PwZ3xSE_By9MdUrk1wiEw_6rcApqsc9Xg7pg4u-ytNWuOI0zbghzaM-4mpUeFsDA0Afa8K9kE2n8cj3X_5VpJICGRhmzeiy8meM1GlyJoUtCRiBVK5ou-r38a4i2oSXV2iSA\" alt=\"output.PNG\" title=\"\"><\/figure>\n\n\n\n<p>The above example was a demonstration of Advance User Interactions API in Selenium Webdriver and the use of Keyboard and Mouse events on the web similar to user\u2019s interaction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&nbsp;<strong>Conclusion<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using AdvancedUserInteractions API, we can handle special keyboard and mouse events.<\/li>\n\n\n\n<li>doubleClick(), keyUp, dragAndDropBy, contextClick &amp; sendKeys are most frequently used Keyword and Mouse Events.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn to handle Mouse Click &amp; Keyboard Event in Selenium Webdriver. Using Advanced User Interactions API, we can handle special mouse and keyboard events in Selenium testing. These API contains Action classes and Actions that are needed when executing keyboard and mouse events. Action class provides the most commonly used [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[718,156,716,717,448,51],"class_list":["post-3136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-actions-class","tag-automation-testing","tag-handling-mouse-click","tag-keyboard-event","tag-selenium-webdriver","tag-software-testing"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3136"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3136\/revisions"}],"predecessor-version":[{"id":34032,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3136\/revisions\/34032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3147"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}