{"id":8396,"date":"2021-02-15T16:56:54","date_gmt":"2021-02-15T11:26:54","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8396"},"modified":"2021-02-15T16:56:56","modified_gmt":"2021-02-15T11:26:56","slug":"double-click-and-right-click-in-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/double-click-and-right-click-in-selenium-webdriver\/","title":{"rendered":"Double Click and Right-Click in Selenium Webdriver"},"content":{"rendered":"\n<p>Double click is a process of clicking the left mouse button twice. This click is mostly performed with the left mouse button, and it opens a new tab, a new folder executing a file, a folder, or a program.<\/p>\n\n\n\n<p>Right-click is a process of single-clicking the right mouse button. It also gives options to open a file, folder, or program.<\/p>\n\n\n\n<p>You can see various web pages where you can find examples of Double click and Right-click can be found. One example of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTML\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">HTML page<\/a> that we have created is given below that shows a Double click and a Right-click button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Dwsb6svHN5RBkX_aAutfLQMI0lyyN1alW57lbBmaSVfRaF4aWUEccQYsmWkcZ-dN0IDlAWbgLLhLmarfBxNlC7SQdN4h1XqeQUijvqk_jz5e8An_GPru_oi4dHIsK2tO6wi7TOo\" alt=\"Double click\" title=\"\"><\/figure>\n\n\n\n<p>The image above shows a Double click button and a Right-click option. While the double click button is clicked twice, a message pops up for double click. After clicking the Right-click option, a list of actions like open link in a new tab, open link in a new window, etc., is displayed.<\/p>\n\n\n\n<p>Check the below code and the images.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html>\n&lt;head>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;title> Clicks Demo &lt;\/title>\n&lt;\/head>\n&lt;div>\u00a0\n&lt;div>\n&lt;button id=\"doubleclick\" ondblclick=\"callOnDoubleClick()\">Double-click this box &lt;\/button>\n&lt;script>\nfunction callOnDoubleClick()\n{\nalert(\"Great ! \\n\\n Double-click performed successfully..\");\n}\n&lt;\/script>\n&lt;\/div>\n&lt;\/div> &lt;br>\n&lt;div>\n&lt;a href=\"file:\/\/\/E:\/Selenium class\/Programs\/Sonali\/bin\/project1\/add.html\" id=\"rightclick\"\n\u00a0oncontextmenu=\"callOnRightClick(event)\">Right-click here&lt;\/a>\n&lt;\/div>\n&lt;\/html>\nHTML code is given below for the tab opened after clicking the Right-click option.\n&lt;html>\n&lt;head>\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;title> HTML Page &lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;form>\n&lt;h2> Mouse click &lt;\/h2>\nGreat!\nRight Click is performed successfully. Also, desired tab\/window is opened.\n&lt;\/form>\n&lt;\/body>\n&lt;\/html>\n<\/pre>\n\n\n\n<p>Pop up that opens after Double click:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/7eUbiaOPW5RgOU8wuretY5B3ITgBBn9dUiDF4VcmEGJEWw1tH5wauBDg0MaRO5RPG1507AS8FTT-JWRU5JKBl7SjwIvhjGtxPONvtuUowRxfW2505_d5cIlvOBBsrpkTBCUtlyI\" alt=\"pop up after clicking double click.\" title=\"\"><\/figure><\/div>\n\n\n\n<p>Options that are displayed after Right-click:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/xyYGT9agVBu_jV8QeAqAbqen-edhuCXXyaepjqzsx5Fqzg5qgc4WzfcZUglcIBg6JZRCj9pCXgBDhL21rzoVt3yJCKOpqUNA5yS2QVjT4gbfegfAoXaoU06nBBcg4uQmt8qlkBI\" alt=\"click_html\" title=\"\"><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/apUoa7NRyDFTBIeq81lfkNLGJYtMKHXWMVl_4zPJYBkfUT1Zt4yu4VHh6gcajObOtQ_JSjdLpd7RdqIW6yIn_yhIkpQmreeYSfJ_DTt1Ac6uIrDvtEQ2YFcmWYijh4euBwENw6Q\" alt=\"html_right\" title=\"\"><\/figure><\/div>\n\n\n\n<p>The below tab opens after choosing the option &#8220;Open link in a new tab&#8221;:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/6sj5d1SQFtXTh1seN5sEagIeaVLXq0JU9xw6djNRt57OJLsAa0YOG7IbOjuU5NNkqHa_D4TmeuNSlbLHyhTPSH_QzG5_Mah0QaHo4EPRCm7Y1eNUUz_COZVHUs4jZd0kJo1Q75Y\" alt=\"Open link in new tab\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Actions Class In Selenium:<\/strong><\/h2>\n\n\n\n<p>After understanding the Double click and Right-click on an HTML page, let us see how they are handled in Selenium.<\/p>\n\n\n\n<p>Consider a scenario of a link, as shown in the below screenshot, where we will handle these clicks using Selenium.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/WBX1YyVRdEoq7mcClfsVC7iasjhJcl5tMYFXKz_aGIRvbAedwhfy7tWnlwcBXXS3fzNgdqSqzDhfCo_FfkpGR265-C7geTMAoeDn0tuVqD0SweJ5NV4D7HmVT09Yk6SzaPZiHNw\" alt=\"Handling Clicks in Selenium\" title=\"\"><\/figure><\/div>\n\n\n\n<p>Initially, when you logged into the website with the username and password, the above dashboard page opens up. Our aim is to perform Double click and Right-click operations on the Admin tab.<\/p>\n\n\n\n<p>Let us see the implementation of the code to handle Double click operation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.firefox.FirefoxDriver;\nimport org.openqa.selenium.interactions.Actions;\u00a0\npublic class DoubleClick {\n\u00a0\u00a0\u00a0\u00a0public static void main(String arg[]) throws InterruptedException {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebDriver dr = new FirefoxDriver();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dr.manage().window().maximize();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dr.get(\"https:\/\/opensource-demo.orangehrmlive.com\/\"); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/testing webpage\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement uname = dr.findElement(By.id(\"txtUsername\"));\u00a0 \u00a0 \/\/username\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uname.sendKeys(\"Admin\");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement pwd = dr.findElement(By.name(\"txtPassword\")); \u00a0 \u00a0 \u00a0 \/\/password\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pwd.sendKeys(\"admin123\");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement login_button = dr.findElement(By.xpath(\"\/\/input[@id='btnLogin']\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0login_button.click();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/loginbutton\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement admin = dr.findElement(By.id(\"menu_admin_viewAdminModule\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Actions act = new Actions(dr);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.sleep(3000);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0act.doubleClick(admin).build().perform();\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.sleep(3000);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre>\n\n\n\n<p>The above code for Double click shows the use of the Actions class. In the above code, Double click is handled in Selenium using the Actions class and is done by creation of an object of Actions class by bypassing the driver.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Actions Class for handling Double click in Selenium:<\/strong><\/h2>\n\n\n\n<p>The web element on which we want to perform Double click (here it is the &#8220;Admin&#8221; tab), and using the Actions class and in-built double-click method, the Double click operation is performed on the web element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What happens after a Double Click Operation?<\/strong><\/h2>\n\n\n\n<p>When an Admin tab is Double clicked, it opens another page, and the opened page can then be viewed, i.e., the System Users page. Thus, after Double-clicking the Admin&#8217;s tab, we are directed to the System Users page.<\/p>\n\n\n\n<p>Check the image for a better understanding.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/EUNRJTvsCYf8Z1dfP4mLPE5eUgEkciWSazj5b-SGt6l4CWXVY-YnZb9wZwSzJCu682nek2oqcaEOQpMuS-agsl10z-FTtfh2QnH8NKaQWGNYqV_BSmrEO3gfYIANOseSN8fLY0E\" alt=\"Double Click Admin Tab\" title=\"\"><\/figure>\n\n\n\n<p>Code For Handling the Right-Click In Selenium:<\/p>\n\n\n\n<p>Now let us see the handling of Right-click using <a href=\"https:\/\/www.h2kinfosys.com\/blog\/selenium-tutorials\/\" class=\"rank-math-link\">Selenium Webdriver<\/a>. Again let us consider the same example and perform Right-click on the &#8220;Admin&#8221; tab.<\/p>\n\n\n\n<p>Let us see the implementation of the code to handle the Right-click operation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.firefox.FirefoxDriver;\nimport org.openqa.selenium.interactions.Actions;\npublic class RightClick {\n\u00a0\u00a0\u00a0\u00a0\u00a0public static void main(String arg[]) throws InterruptedException {\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebDriver dr = new FirefoxDriver();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dr.manage().window().maximize();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dr.get(\"https:\/\/opensource-demo.orangehrmlive.com\/\"); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/testing webpage\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement uname = dr.findElement(By.id(\"txtUsername\"));\u00a0 \u00a0 \u00a0 \/\/username\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uname.sendKeys(\"Admin\");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement pwd = dr.findElement(By.name(\"txtPassword\")); \u00a0 \u00a0 \/\/password\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pwd.sendKeys(\"admin123\");\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement login_button = dr.findElement(By.xpath(\"\/\/input[@id='btnLogin']\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0login_button.click(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/loginbutton\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebElement admin = dr.findElement(By.id(\"menu_admin_viewAdminModule\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Actions act = new Actions(dr);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.sleep(3000);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0act.contextClick(admin).build().perform();\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.sleep(3000);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre>\n\n\n\n<p>In Selenium Webdriver, Right-click is also called a Context-click. The above Right click is performed on the &#8220;Admin&#8221; tab using the Actions class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Actions Class for handling Right-click in Selenium:<\/strong><\/h2>\n\n\n\n<p>The web element on which we want to perform Right-click (here it is the &#8220;Admin&#8221; tab), and using the Actions class and in-built right-click method, a Right-click operation is performed on the web element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What happens after a Right-click operation?<\/strong><\/h2>\n\n\n\n<p>When a &#8220;Admin&#8221; tab is Right clicked, it displays a list of actions to choose from.<\/p>\n\n\n\n<p>Thus, after Right-clicking the Admin&#8217;s tab, we get the following options:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Open link in new tab<\/li><li>Open link in new window<\/li><li>Open link in a new private window<\/li><li>Bookmark the link<\/li><li>Save the link<\/li><li>Copy link location<\/li><li>Search Google for \u201cAdmin\u201d and<\/li><li>Open element Inspector<\/li><\/ol>\n\n\n\n<p>Take a look at the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/dDtTVEE-A0D-0K7BBEsNMJv9HIOjUq1eVwvkUE1rZiHHH_yJfQz2CPYMYY8BCRnOrdPSDIa-Pl5Oo7MRQvvPz5QDkxQFAUmdNvZyau-_AKvM2w9vVS46PbpQ3VI0GSdJu9pWK9s\" alt=\"Page after Right Click operation\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Difference Between Double-Click And Right-Click:<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Double click<\/strong><\/td><td><strong>Right Click<\/strong><\/td><\/tr><tr><td>Double click is nothing but performing a Left mouse click twice.<\/td><td>Right-click is performing a single Right mouse click.<\/td><\/tr><tr><td>This directly interacts with an object.<\/td><td>This indirectly interacts with an object.<\/td><\/tr><tr><td>It is also known as a normal click or regular click.<\/td><td>It is also known as Context Click.<\/td><\/tr><tr><td>Example: Double-clicking of a link opens it immediately after the click.<\/td><td>Example: Right-clicking a link shows options to act on the link like Open link in a new tab, opening in a new window, copy link, save the link, etc.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Double click is a process of clicking the left mouse button twice. This click is mostly performed with the left mouse button, and it opens a new tab, a new folder executing a file, a folder, or a program. Right-click is a process of single-clicking the right mouse button. It also gives options to open [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-8396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8396","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8396"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8396\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8400"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}