{"id":3517,"date":"2025-01-16T19:39:48","date_gmt":"2025-01-16T19:39:48","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/handle-double-click-drag-and-drop-using-playwright-java\/"},"modified":"2025-01-16T19:39:48","modified_gmt":"2025-01-16T19:39:48","slug":"handle-double-click-drag-and-drop-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/handle-double-click-drag-and-drop-using-playwright-java\/","title":{"rendered":"Handle \u2018double click\u2019, \u2018drag and drop\u2019 using Playwright Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3517\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java to handle double click, drag and drop.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul class=\"\">\n<li>Handle double click<\/li>\n<li>Handle drag and drop<\/li>\n<li>Source code (double click)<\/li>\n<li>Source code (drag and drop)<\/li>\n<\/ul>\n<p><strong>Handle \u2018double click\u2019<\/strong><\/p>\n<p>To understand double click, go to https:\/\/api.jquery.com\/dblclick\/\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a2708d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3520\" data-css=\"tve-u-19470a27f41\" data-height=\"260\" data-id=\"3520\" data-init-height=\"256\" data-init-width=\"406\" data-width=\"413\" decoding=\"async\" height=\"260\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.10.48\u202fAM.png\" style=\"aspect-ratio: auto 406 \/ 256;\" title=\"Screenshot 2025-01-17 at 1.10.48\u202fAM\" width=\"413\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0As can be seen above, there is a blue color block that we can double click.<\/p>\n<p>When we double click, the color changes to yellow<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a378ea\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3521\" data-css=\"tve-u-19470a38876\" data-height=\"221\" data-id=\"3521\" data-init-height=\"256\" data-init-width=\"406\" data-width=\"350\" decoding=\"async\" height=\"221\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.11.48\u202fAM.png\" style=\"aspect-ratio: auto 406 \/ 256;\" title=\"Screenshot 2025-01-17 at 1.11.48\u202fAM\" width=\"350\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0When we double click again, the color changes back to blue<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a3f449\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3522\" data-height=\"256\" data-id=\"3522\" data-init-height=\"256\" data-init-width=\"406\" data-width=\"406\" decoding=\"async\" height=\"256\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.12.31\u202fAM.png\" style=\"aspect-ratio: auto 406 \/ 256;\" title=\"Screenshot 2025-01-17 at 1.12.31\u202fAM\" width=\"406\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us see how to automate this event.\u00a0<\/p>\n<p>Let us inspect the block and copy the xpath<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a478b1\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3523\" data-css=\"tve-u-19470a487d2\" data-height=\"306\" data-id=\"3523\" data-init-height=\"598\" data-init-width=\"1162\" data-width=\"594\" decoding=\"async\" height=\"306\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.13.03\u202fAM.png\" style=\"aspect-ratio: auto 1162 \/ 598;\" title=\"Screenshot 2025-01-17 at 1.13.03\u202fAM\" width=\"594\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So we can locate this block using below xpath:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a50048\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3524\" data-css=\"tve-u-19470a5115a\" data-height=\"88\" data-id=\"3524\" data-init-height=\"116\" data-init-width=\"572\" data-width=\"436\" decoding=\"async\" height=\"88\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.13.35\u202fAM.png\" style=\"aspect-ratio: auto 572 \/ 116;\" title=\"Screenshot 2025-01-17 at 1.13.35\u202fAM\" width=\"436\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The next action is to double click this box.<\/p>\n<p>The below code snippet will do the needful<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a63053\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3525\" data-css=\"tve-u-19470a64e17\" data-height=\"597\" data-id=\"3525\" data-init-height=\"1290\" data-init-width=\"1174\" data-width=\"543\" decoding=\"async\" height=\"597\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.14.40\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 1290;\" title=\"Screenshot 2025-01-17 at 1.14.40\u202fAM\" width=\"543\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The color changes to yellow (due to first double click)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a773d6\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3526\" data-css=\"tve-u-19470a78594\" data-height=\"822\" data-id=\"3526\" data-init-height=\"1574\" data-init-width=\"1174\" data-width=\"613\" decoding=\"async\" height=\"822\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.15.48\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 1574;\" title=\"Screenshot 2025-01-17 at 1.15.48\u202fAM\" width=\"613\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Handle \u2018drag and drop\u2019<\/strong><\/p>\n<p>Let us now navigate to https:\/\/jqueryui.com\/resources\/demos\/droppable\/default.html\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a89a2d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3527\" data-css=\"tve-u-19470a8af57\" data-height=\"538\" data-id=\"3527\" data-init-height=\"1222\" data-init-width=\"1174\" data-width=\"517\" decoding=\"async\" height=\"538\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.17.30\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 1222;\" title=\"Screenshot 2025-01-17 at 1.17.30\u202fAM\" width=\"517\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us see how to automate this event.<br \/>The small box that needs to be dragged has a horizontal x-axis and vertical y-axis.<br \/>Let us inspect this box that we need to drag and copy the css selector\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470a9e013\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3528\" data-css=\"tve-u-19470a9fc20\" data-height=\"668\" data-id=\"3528\" data-init-height=\"1282\" data-init-width=\"1174\" data-width=\"612\" decoding=\"async\" height=\"668\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.18.53\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 1282;\" title=\"Screenshot 2025-01-17 at 1.18.53\u202fAM\" width=\"612\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So we can write<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470ab94ae\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3529 tcb-moved-image\" data-css=\"tve-u-19470aba068\" data-height=\"69\" data-id=\"3529\" data-init-height=\"136\" data-init-width=\"1174\" data-width=\"600\" decoding=\"async\" height=\"69\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.20.17\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 136;\" title=\"Screenshot 2025-01-17 at 1.20.17\u202fAM\" width=\"600\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The next action is to move mouse to middle of the smaller box so that we can drag it<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470ad7edd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3530\" data-css=\"tve-u-19470ad92f3\" data-height=\"580\" data-id=\"3530\" data-init-height=\"1076\" data-init-width=\"1174\" data-width=\"633\" decoding=\"async\" height=\"580\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.22.44\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 1076;\" title=\"Screenshot 2025-01-17 at 1.22.44\u202fAM\" width=\"633\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The below code snippet will move mouse to the middle of drop box<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470ae1c92\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3531\" data-css=\"tve-u-19470ae390d\" data-height=\"73\" data-id=\"3531\" data-init-height=\"142\" data-init-width=\"1174\" data-width=\"606\" decoding=\"async\" height=\"73\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.23.32\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 142;\" title=\"Screenshot 2025-01-17 at 1.23.32\u202fAM\" width=\"606\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We will than fire an event to move the mouse down<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470af0e46\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3532\" data-css=\"tve-u-19470af1e62\" data-height=\"354\" data-id=\"3532\" data-init-height=\"706\" data-init-width=\"1174\" data-width=\"588\" decoding=\"async\" height=\"354\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.24.14\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 706;\" title=\"Screenshot 2025-01-17 at 1.24.14\u202fAM\" width=\"588\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>So logic will be<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470af7e1d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3533\" data-css=\"tve-u-19470af981c\" data-height=\"335\" data-id=\"3533\" data-init-height=\"664\" data-init-width=\"1174\" data-width=\"593\" decoding=\"async\" height=\"335\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.25.05\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 664;\" title=\"Screenshot 2025-01-17 at 1.25.05\u202fAM\" width=\"593\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>Notice below the drag and drop operation is success<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470b0001b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3534\" data-css=\"tve-u-19470b01315\" data-height=\"352\" data-id=\"3534\" data-init-height=\"664\" data-init-width=\"1174\" data-width=\"622\" decoding=\"async\" height=\"352\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-1.25.39\u202fAM.png\" style=\"aspect-ratio: auto 1174 \/ 664;\" title=\"Screenshot 2025-01-17 at 1.25.39\u202fAM\" width=\"622\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how we can handle double click and drag\/drop events using playwright java.<\/p>\n<p><strong>Source code (double click)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog30_DblClick_PWJava {<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/api.jquery.com\/dblclick\/&#8221;);<\/p>\n<p>\/\/dbl click to change color from blue to yellow<\/p>\n<p>page.frameLocator(&#8220;\/\/iframe&#8221;).locator(&#8220;\/\/html\/body\/div&#8221;).dblclick();<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(1000);<\/p>\n<p>} <strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>\/\/dbl click to change color from yellow to blue\u00a0<\/p>\n<p>page.frameLocator(&#8220;\/\/iframe&#8221;).locator(&#8220;\/\/html\/body\/div&#8221;).dblclick();<\/p>\n<p>page.pause();<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (drag and drop)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog30_DragDrop_PWJava {<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p><\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/jqueryui.com\/resources\/demos\/droppable\/default.html&#8221;);<\/p>\n<p>Locator draggable = page.locator(&#8220;#draggable&#8221;);<\/p>\n<p>Locator droppable = page.locator(&#8220;#droppable&#8221;);<\/p>\n<p>page.mouse().move(draggable.boundingBox().x + draggable.boundingBox().width\/2, draggable.boundingBox().y + draggable.boundingBox().height\/2);<\/p>\n<p>page.mouse().down();<\/p>\n<p>page.mouse().move(droppable.boundingBox().x+ droppable.boundingBox().width\/2, droppable.boundingBox().y + droppable.boundingBox().height\/2);<\/p>\n<p>page.mouse().up();<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(1000);<\/p>\n<p>} <strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>page.pause();<\/p>\n<p>\/\/page.close();<\/p>\n<p>\/\/playwright.close();<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to handle double click, drag and drop. Topics that we will cover: Handle double click Handle drag and drop Source code (double click) Source code (drag and drop) Handle \u2018double click\u2019 To understand double click, go to https:\/\/api.jquery.com\/dblclick\/\u00a0 \u00a0As can be seen above, there is a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3517","post","type-post","status-publish","format-standard","hentry","category-java","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3517","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=3517"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3517\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}