{"id":2607,"date":"2023-10-31T01:47:00","date_gmt":"2023-10-31T01:47:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2607"},"modified":"2023-12-03T06:50:48","modified_gmt":"2023-12-03T06:50:48","slug":"action-class-mouse-hover-operation-in-selenium-and-keypress-events","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/10\/31\/action-class-mouse-hover-operation-in-selenium-and-keypress-events\/","title":{"rendered":"Action class Mouse Hover operation in Selenium and keypress events"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>Perform Mouse Hover with the help of Actions class \u2013 Example 1<\/li>\n\n\n\n<li>Example 2 of mouse hover<\/li>\n\n\n\n<li>Keypress events (Control+a)<\/li>\n\n\n\n<li>Keypress events (Control+c)<\/li>\n\n\n\n<li>TAB key<\/li>\n\n\n\n<li>Keypress events (Control+v)<\/li>\n\n\n\n<li>Source code (mouse hover)<\/li>\n\n\n\n<li>Source code (keypress)&nbsp;<\/li>\n<\/ol>\n\n\n\n<p><strong><br><\/strong><strong>Perform Mouse Hover with the help of Actions class \u2013 Example 1<\/strong><\/p>\n\n\n\n<p>Launch <a href=\"https:\/\/www.spicejet.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.spicejet.com\/<\/a>&nbsp;<\/p>\n\n\n\n<p>When we mouse hover \u2018Add-ons\u2019 menu, a child sub-menu automatically comes up<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/TR4AkofPJVlt8kEUd2OyAKKYl-q_VqLivrGmCA_X_FAc_bF8in0u3ykPn_BCaAdLX7npHB25V2gQk5uNCXAF_daP1VJgp_YOPGBuvxB0Gx-nJjLUS0QFRZrsUyryTvNFvB31HS3-xAWhepo_Eccv\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us automate a scenario where the user mouse-hovers \u2018Add-ons\u2019 and clicks \u2018SpiceMax\u2019 sub-menu. In Selenium, this mouse hover operation can be achieved by using \u2018Actions\u2019 class.<\/p>\n\n\n\n<p>Let us inspect \u2018Add-ons\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6-vND-TWK3_pQw6jq67fK1L6EimEwUBi9DhqwLJezhLyyQVVj_a1GzliMsQ4ioOxgNeQKuowo0xM6XoD1KAg-BISkAYvnPPo3tWzkfzyBsl_Q3iR9pHAgOl1JV2spafOLCAVEJaUz4dZNNus5R0f\" alt=\"\"\/><\/figure>\n\n\n\n<p>Also let us inspect \u2018SpiceMax\u2019 child sub-menu<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/8uIlgg-0_6gGHRpkcOzdvnbo2DzNr82AOxI1Ft2CSIbsnkowB0N18_wXYm44kJ45C4OHUuB_4ZWsE8yJ3PC6OfgfajMjKFfO8qazYb685hDGjbGqJ8IxIAALa8RGAjNLZCB39hTyDQjfls7EQ1w_\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can now write our code.&nbsp;<\/p>\n\n\n\n<p>In line#16, the driver object is passed in constructor of Actions class. The driver reference \u2018act\u2019 now gets all the capabilities of Actions\u2019s class methods, example, <em>moveToElement()<\/em> method<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/v2x0mBWcqNQByrhVJn1wYkviQT1PN8mx1NMR448tqyNxX52sf7DnfWfmnVfBDosGGmrbybXk4MjMsBmJpww4r6Dk1egr4ic7yhIEb6KvvbWqpP8Ixjai0hZgS3uFINnk3UP7Je-rCW4CpsCEuZtN\" alt=\"\"\/><\/figure>\n\n\n\n<p>In line#17, the method \u2018moveToElement\u2019 uses the \u2018act\u2019 reference\u2019. This helps in moving to \u2018Add-ons\u2019 parent menu.&nbsp;<\/p>\n\n\n\n<p>Also, in line#17, we have to append the methods <em>.build().perform()<\/em> at the end, otherwise mouse hover will not happen.<\/p>\n\n\n\n<p>Execute the script. The script was able to successfully mouse hover and click the sub-menu \u2018SpiceMax\u2019 item as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/xA2wBd_gO_iAR4iH_5Mi762BJbMpr2aYEKrvryakK3nMg8KUz1VqnOCkW1XmLOjte_lMnk-gsZmgSgBVhCTC-mMML2HmXxXapwBDK2MQXJWJcZOfMictP4ARZEnjTfWwHz376kTyGlnprbRYS6FB\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Example 2 of mouse hover<\/strong><\/p>\n\n\n\n<p>Let us see another example. Launch <a href=\"https:\/\/www.yahoo.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.yahoo.com\/<\/a>&nbsp;<\/p>\n\n\n\n<p>When we mouse-hover \u2018More\u2019 parent item, a lot of child sub-menu items get displayed&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/R90bo7X0ylTnr15tQ3wM3YkBNwEDFd0uqb5ZzAYWEyBNNU9KqZ3L9SQGTrbcL4Y2szjWofJUaXemd5qq3L3hyLB7B6HtzylDq8S3LZqeWenzIERwig7_UZpTnPAAONlDIhw9V_mWnlKvK9wvG2Az\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us inspect \u2018More\u2019. It is represented by \u2018span\u2019 tag having text \u2018more\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/8ZCwpAQgGIUDW5Yhtn96CiEjHEXMs1E3XIc39Z7TWqO6aExbtxTeKqOORqTQzDslqWOwYnfk_3bv3B1k-iAR-Zawt_E0wNCp4o6DBtWOnOJSONMejunMHxvpb9wZ9wOdHBKHmH02vBMBSqYfFE8f\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us inspect \u2018Parenting\u2019 under <em>More &gt; Life<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CKNCnk44cJFQAhQnsCvEZO2wHilainO-0NEB9IwUdXY7P2GGwZiMZeAtlCFQGwkvSYtj8_ACUp4zAr3cZ9GEcRTkD64B5Ml5A9iAd2JhZWIquAXtGB3iT0tnByMDT-Ovh2-W-SQ-hzIbbcmJf-Pc\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can use \u2018linkText\u2019 locator<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/DzY0L2yfloVL94vhsUKBwvbUhH3rJsvz0qoBT_5ogvnO3Ah0AsRyHYTEiBwHpkZCMfzRdYLLxJUe5l0akE0xWxtsMjqmAX9vfhxJb9sTErDh8eCUEDlFiMTwKSWLl5JwwpqlAYEIepY3Bj3foNlV\" alt=\"\"\/><\/figure>\n\n\n\n<p>So our script would be:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/5V1m9Ocr05RXhBYyienoijX-gZiixQ0BClUj1UfdPqhmVX8sdgFDdLU2MHkWbNPE7yoATA2W42LYxIp9mUTAzS3yr4iDwMfpra6Oh29_MjE9K6GZiECgw9iIh5EgcivK1GGzn7JNCWBzuvwJ2E8C\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save and run the script.&nbsp;<\/p>\n\n\n\n<p>Notice below that the parenting page opens<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/QbsI6-pE_f5m6mjQt_DIMbLvamqQe0ldO4KJv6_cDpv6CA1qyzZGf-6PId8AanZorB2NsJOfkZ1MoPN6HC4XABgz7MwhUZiEnwlCfrB1u7QC2vCL4ja1X-5LtuoPcybPKZfwvtfFFdrMg4IrdajN\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Keypress events (Control+a)<\/strong><\/p>\n\n\n\n<p>Launch <a href=\"https:\/\/text-compare.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/text-compare.com\/<\/a>&nbsp;<\/p>\n\n\n\n<p>There are 2 textbox areas on this page, one on the left hand side and another on right hand side<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/bMsYBMZn5Fp2IFM2r1Hv6xE-hE_DYg71NiCyW__f8bhTxat2ke7ysfUJ5OhWXQkAGDNr9BjOLKKwY1O5H9SQmji-X9RrFOiNw4n9m8PmVquqDWdFAT6siUCZlGRrTrl0OXx4zN5upZoy82VLC0Cz\" alt=\"\"\/><\/figure>\n\n\n\n<p>Use case that we are going to automate:<\/p>\n\n\n\n<p>The script will write some text in the left area, will select the entire text using \u201cctrl+a\u201d, will copy the entire text using ctrl+c, will shift to the right area using \u2018tab\u2019 key and finally paste the text in the right area using \u201cctrl+v\u201d. This way, we will have same text both the text areas. So let us see how to automate this scenario.<\/p>\n\n\n\n<p>Inspect left had side text area<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/owj-OUN0xpwGi5w3udiDR01MTIU1hf5oDOzgw0CcQsAr6Mvz8hYaEn4SFjzb3leRg-5Eb-2AwaaIJvBpz_qZltoQIpv-yOPjTkF1NnNQ6Btksj9OO1m30MrgvV8MNcnWFaQoO67OVbtfHEN5ekUi\" alt=\"\"\/><\/figure>\n\n\n\n<p>Inspect right had side text area<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/OZ8_PnMMAFqc0z9DBddPVn0HEbjLTHpCT26RiGCryLARZf8swLWn3V3pHX50ySXFPL30wy68Lbo83evQ6wXkZ5u-QhMpHFT8-Gg34dwylyqcClsGo2UxOMiwU6IZZJHQSOsPOd1DSaG24PjNyU4x\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us write some text on the left had side text area using the <em>sendkeys()<\/em> method, see line#23 shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/gUO-HG6CyzMvFVH2hYWAYxIPfrA1jYjeVRyzK2HcvVB5fBFsoyImmZVVq6P94Jdkru38pk-JazQWrjS033O-sXhgG3k62XYo5TK0zqhVpISBJfGtJ6pUt6zmFzKbTGwvZGWQJKXcvY5r5PZqsJQI\" alt=\"\"\/><\/figure>\n\n\n\n<p>After writing the text, the script will perform \u2018select-all\u2019 operation using ctrl+a (see lines 28-31 above).&nbsp;<\/p>\n\n\n\n<p>Execute the script, notice below that the desired text gets typed in the left area<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/qGyAMebVau-xvMqw-_B-tNARWPuZSJlyuGxOHzaHRo7Hg0SUTlaLzkorvL2qfZW5hOzijCgx3Vl3L3OUMVVl_v_0IjCQWtgYzdFe8kOxXaBTvOb67V7y_eEh8_dmIj1kHQi-qmwfwEHLaLaBrq-5\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Keypress events (Control+c)<\/strong><\/p>\n\n\n\n<p>The below steps would perform the \u2018copy\u2019 operation<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/GfxclZCHi4UbhN03Gga0ddZabYUdw5GY6g2e3TWSWssLwOV7haBRqQZvDJ8TqxlGKNe0aKHRDLR-LkspMl79CeUUdujizYDEUV3NJjsNKdFlroOXcTAr07lEuirw4vPE0j3cBLYax90_zzbglqwR\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TAB key<\/strong><\/p>\n\n\n\n<p>The below steps would perform the \u2018tab\u2019 operation<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/5d0_nM36GNR91KaMdtvqputnSdGsSDUes8feWFh9IG_aaV0H17eKbnKOPtCxpZ5s0WBBtAz6ulMqZYvfRhx8frjq0xkXuSTRg_44hO_tPyDZMEGO1CyeyzYT_6sTenLX-wKOczdPr69ZiZ9n2pRe\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Keypress events (Control+v)<\/strong><\/p>\n\n\n\n<p>The below steps would perform the \u2018paste\u2019 operation<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/wG9YdROMvttu3AyuOxsgVM84ec_JQwJTKno1asIIhluLSKzvyzJMZ1mRXkTWyovF6uA9YqD8ACn8p8A_XFFaGGMtqWtTBtEMmjaRJGLYy2ASWKMyjLnIgv-tgT7vZGF11WvkYD9Npczwn3pVM0MW\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute the script.&nbsp;<\/p>\n\n\n\n<p>Notice that the same text gets copied in the right area<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/QW6sd9-T03BaoMEvctXV5iiFP0lsOSoqzWiB5y-ws_0EEMdUhgD3eKnalOVcbySvQb93vIGYY9EaTMntHuvb5-NzesfaPKzMGJiZpu2JCBUK0hyJddl86VoAfIVl-oX3f4zwNLWsq9KbzIWttKgg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Similarly you can try to experiment with other keypress operations<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/BAzKcM_3RylNGJKN2HZsc7BpiNe1gFkTSVLGmAvYgtnY7uacGi3bpga9RXVmzRWy97vlxSHmTqqe0VxbMYrdQ9JHHBWiPTB1c_QNmBfuWfoV6DSs42I2Egb0F2XS4T3K0OAXbstaim87h-7WF9fD\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Source code (mouse hover)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/yb87uoPLzU5wJ3-krdX-3W8IeZKCXoYAKaO1X9UJVhry1p2LxzYHbBxs7yLtKzN7H2tR8ycQ23pZA_XtPUXzAHWvWKbhTSHaBFgdwQhVfmzRnMSC3ED051412ZocWkQncsasjp0dMyPXpMBZXpam\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Source code (keypress)&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/q6Czja38ClaaH5xBaHd5Llma6yjTd4V_x2lxlcy3zPnTgMpFr4WWqNB5y6I6e6oz2U_NBNwAx-tGgK-Z_kitL_rWAHSTh9M3FHWfH2nkNlakenZypdv3mQ7qG29hI3LUhJRQo9w71LtWp2L9g58N\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/A8xreBFihDxuOuYV25IKjxbEz-miNCVeq4c-6cUfDJb-kWaU4_EmYYCnOJGKSGC57wYk9lJGQb10uBeUY7HLnTZgWYkNu9hHDD2aFUdHsiZFqX2Pe3KnM33nQJXVZJPA90AUVngF7BmF1U8Tx30r\" alt=\"\"\/><\/figure>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perform Mouse Hover with the help of Actions class \u2013 Example 1 Launch https:\/\/www.spicejet.com\/&nbsp; When we mouse hover \u2018Add-ons\u2019 menu, a child sub-menu automatically comes up Let us automate a scenario where the user mouse-hovers \u2018Add-ons\u2019 and clicks \u2018SpiceMax\u2019 sub-menu. In Selenium, this mouse hover operation can be achieved by using \u2018Actions\u2019 class. Let us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2607","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2607"}],"version-history":[{"count":11,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2607\/revisions"}],"predecessor-version":[{"id":2820,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2607\/revisions\/2820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}