

{"id":113964,"date":"2023-07-29T19:10:59","date_gmt":"2023-07-29T13:40:59","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=113964"},"modified":"2023-07-29T19:10:25","modified_gmt":"2023-07-29T13:40:25","slug":"drag-and-drop-in-selenium","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/","title":{"rendered":"Drag and Drop in Selenium"},"content":{"rendered":"<p>Selenium WebDriver is a powerful tool that is widely used for web application testing. It has many features that make it an excellent choice for testing web applications, including the ability to automate drag and drop operations. In this article, we will explore the basics of drag and drop in Selenium WebDriver.<\/p>\n<h3>Drag and Drop in Selenium<\/h3>\n<p>Drag and drop is a common interaction pattern in modern web applications. It involves clicking and holding an element with the mouse cursor, moving it to a new location, and then releasing it. Drag and drop testing is the process of testing this interaction pattern to ensure that it works as expected.<\/p>\n<p>Selenium WebDriver provides a simple and intuitive way to automate drag and drop testing. It offers several methods that allow you to simulate drag and drop interactions using the mouse cursor. In the following sections, we will explore the different methods provided by WebDriver for drag and drop testing.<\/p>\n<h3>Ways to drag and drop in Selenium<\/h3>\n<h4>Method 1: Using Actions Class<\/h4>\n<p>The Actions class in Selenium WebDriver provides a way to simulate complex user interactions, including drag and drop operations. It allows you to create a sequence of actions that can be executed on a web page. To use the Actions class for drag and drop testing, you need to create an instance of the class and then use its dragAndDrop() method.<\/p>\n<p>Here&#8217;s an example of using the Actions class for drag and drop testing:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Actions actions = new Actions(driver);\r\nWebElement source = driver.findElement(By.id(\"source\"));\r\nWebElement target = driver.findElement(By.id(\"target\"));\r\nactions.dragAndDrop(source, target).build().perform();<\/pre>\n<p>In this example, we create an instance of the Actions class and then use its dragAndDrop() method to simulate a drag and drop operation. We specify the source and target elements using their IDs, which are obtained using the By.id() method. Finally, we call the build() method to create the sequence of actions and the perform() method to execute them.<\/p>\n<h4>Method 2: Using DragAndDrop() Method<\/h4>\n<p>The DragAndDrop() method is a simple and intuitive way to perform drag and drop operations in Selenium WebDriver. It allows you to specify the source and target elements using their IDs, and then simulates the drag and drop interaction.<\/p>\n<p>Here&#8217;s an example of using the DragAndDrop() method for drag and drop testing:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">WebElement source = driver.findElement(By.id(\"source\"));\r\nWebElement target = driver.findElement(By.id(\"target\"));\r\nActions builder = new Actions(driver);\r\nbuilder.clickAndHold(source).moveToElement(target).release().build().perform();<\/pre>\n<p>In this example, we use the DragAndDrop() method to simulate a drag and drop operation. We specify the source and target elements using their IDs, which are obtained using the By.id() method. We then use the Actions class to create a sequence of actions that simulate the click and hold, move to the target, and release interactions. Finally, we call the build() method to create the sequence of actions and the perform() method to execute them.<\/p>\n<h4>Method 3: Using JavaScriptExecutor<\/h4>\n<p>The JavaScriptExecutor interface in Selenium WebDriver allows you to execute JavaScript code on a web page. It provides a way to simulate drag and drop interactions using JavaScript code.<\/p>\n<p>Here&#8217;s an example of using the JavaScriptExecutor interface for drag and drop testing:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">JavascriptExecutor js = (JavascriptExecutor) driver;\r\nWebElement source = driver.findElement(By.id(\"source\"));\r\nWebElement target = driver.findElement(By.id(\"target\"));\r\nString script = \"function createEvent(typeOfEvent) {\\n\" +\r\n\"var event = document.createEvent(\\\"CustomEvent\\\");\\n\" +\r\n\"event.initCustomEvent(typeOfEvent, true, true, null);\\n\" +\r\n\"event.dataTransfer = {\\n\" +\r\n\"data: {},\\n\" +\r\n\"setData: function (key, value) {\\event.data[key] = value;\\n\" +\r\n\"},\\n\" +\r\n\"getData: function (key) {\\n\" +\r\n\"return event.data[key];\\n\" +\r\n\"}\\n\" +\r\n\"};\\n\" +\r\n\"return event;\\n\" +\r\n\"}\\n\" +\r\n\"function dispatchEvent(element, event, transferData) {\\n\" +\r\n\"if (transferData !== undefined) {\\n\" +\r\n\"event.dataTransfer = transferData;\\n\" +\r\n\"}\\n\" +\r\n\"if (element.dispatchEvent) {\\n\" +\r\n\"element.dispatchEvent(event);\\n\" +\r\n\"} else if (element.fireEvent) {\\n\" +\r\n\"element.fireEvent(\"on\" + event.type, event);\\n\" +\r\n\"}\\n\" +\r\n\"}\\n\" +\r\n\"function simulateDragAndDrop(sourceNode, destinationNode) {\\n\" +\r\n\"var dragStartEvent = createEvent('dragstart');\\n\" +\r\n\"dispatchEvent(sourceNode, dragStartEvent);\\n\" +\r\n\"var dropEvent = createEvent('drop');\\n\" +\r\n\"dispatchEvent(destinationNode, dropEvent, dragStartEvent.dataTransfer);\\n\" +\r\n\"var dragEndEvent = createEvent('dragend');\\n\" +\r\n\"dispatchEvent(sourceNode, dragEndEvent, dropEvent.dataTransfer);\\n\" +\r\n\"}\\n\" +\r\n\"var source = arguments[0];\\n\" +\r\n\"var target = arguments[1];\\n\" +\r\n\"simulateDragAndDrop(source, target);\";\r\njs.executeScript(script, source, target);<\/pre>\n<p>In this example, we use the JavaScriptExecutor interface to execute a custom JavaScript function that simulates a drag and drop operation. The function creates three custom events: dragstart, drop, and dragend. It then dispatches these events on the source and target elements using the dispatchEvent() method. Finally, we call the simulateDragAndDrop() function and pass the source and target elements as arguments.<\/p>\n<h3>Difference between Drag and Drop vs Action Class Build<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Feature<\/b><\/td>\n<td><b>Drag and Drop<\/b><\/td>\n<td><b>Action Class Build<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Interaction<\/span><\/td>\n<td><span style=\"font-weight: 400\">Simulates clicking and dragging an element to a new location<\/span><\/td>\n<td><span style=\"font-weight: 400\">Provides a way to perform more complex interactions with web elements<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Usage<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for simple interactions like moving objects from one location to another or reordering a list of items<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for complex interactions like hovering, right-clicking, or double-clicking<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Code<\/span><\/td>\n<td><span style=\"font-weight: 400\">Requires more code to perform in Selenium using the Actions class<\/span><\/td>\n<td><span style=\"font-weight: 400\">Provides more fine-grained control over the interaction, but can be simpler for Drag and Drop using the dragAndDrop method<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Type of Class<\/span><\/td>\n<td><span style=\"font-weight: 400\">Not a specific class in Selenium<\/span><\/td>\n<td><span style=\"font-weight: 400\">Actions class provided by Selenium<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Examples<\/span><\/td>\n<td><span style=\"font-weight: 400\">Moving a slider, rearranging items in a list<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hovering over a dropdown menu, right-clicking on an element, double-clicking on an element<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Methods of Action Class<\/h3>\n<p>The Actions class in Selenium provides a variety of methods for performing complex interactions with web elements. Here are some of the most commonly used methods of the Actions class:<\/p>\n<p><strong>1. click():<\/strong> Clicks on the specified web element.<\/p>\n<p><strong>2. sendKeys():<\/strong> Sends the specified keys to the active element on the page.<\/p>\n<p><strong>3. moveToElement():<\/strong> Moves the mouse pointer to the center of the specified element.<\/p>\n<p><strong>4. contextClick():<\/strong> Performs a right-click on the specified element.<\/p>\n<p><strong>5. doubleClick():<\/strong> Performs a double-click on the specified element.<\/p>\n<p><strong>6. dragAndDrop():<\/strong> Drags an element from its current position to the center of another element, then drops it.<\/p>\n<p><strong>7. dragAndDropBy():<\/strong> Drags an element from its current position by the specified offset and drops it.<\/p>\n<p><strong>8. keyDown():<\/strong> Presses a specified key down on the keyboard.<\/p>\n<p><strong>9. keyUp():<\/strong> Releases a specified key on the keyboard.<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, drag and drop is an important aspect of web application testing. Selenium WebDriver provides several methods that allow you to automate drag and drop. The Actions class, DragAndDrop() method, and JavaScriptExecutor interface are all effective ways to simulate drag and drop interactions. By using these methods, you can ensure that your web application works as expected and provides a great user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selenium WebDriver is a powerful tool that is widely used for web application testing. It has many features that make it an excellent choice for testing web applications, including the ability to automate drag&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":114563,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22825],"tags":[27584],"class_list":["post-113964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-drag-and-drop-in-selenium"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Drag and Drop in Selenium - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about different ways to perform drag and drop operation in Selenium like using action class, Using DragAndDrop() Method etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Drag and Drop in Selenium - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about different ways to perform drag and drop operation in Selenium like using action class, Using DragAndDrop() Method etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-29T13:40:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Drag and Drop in Selenium - DataFlair","description":"Learn about different ways to perform drag and drop operation in Selenium like using action class, Using DragAndDrop() Method etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/","og_locale":"en_US","og_type":"article","og_title":"Drag and Drop in Selenium - DataFlair","og_description":"Learn about different ways to perform drag and drop operation in Selenium like using action class, Using DragAndDrop() Method etc.","og_url":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-07-29T13:40:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Drag and Drop in Selenium","datePublished":"2023-07-29T13:40:59+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/"},"wordCount":879,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp","keywords":["Drag and Drop in Selenium"],"articleSection":["Selenium Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/","url":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/","name":"Drag and Drop in Selenium - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp","datePublished":"2023-07-29T13:40:59+00:00","description":"Learn about different ways to perform drag and drop operation in Selenium like using action class, Using DragAndDrop() Method etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/webdriver-drag-and-drop-1.webp","width":1200,"height":628,"caption":"selenium webdriver drag and drop"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/drag-and-drop-in-selenium\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Selenium Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/selenium-tutorials\/"},{"@type":"ListItem","position":3,"name":"Drag and Drop in Selenium"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=113964"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113964\/revisions"}],"predecessor-version":[{"id":117114,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113964\/revisions\/117114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/114563"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=113964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=113964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=113964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}