How to drag and drop multiple elements in Selenium Python?
I want to drag and drop multiple elements on a webpage.
For example, I have a grid of images on a webpage. I need to click on an image, drag and drop it in some other grid on that page.
How can I do this with Selenium in Python? You can drag and drop with a combination of JavaScript/JQuery, CSS and Selenium WebDriver. If you want to be able to control the order that you drop the element in the grid, you need to look at JQuery sortable. However, you can get around this by simply grabbing the element from the DOM and placing it in a fixed location on the page. In your case, the location would be off to the side.
The key thing is to be able to identify the element that you want to drop. You will need to look at the CSS selector for the element. This can be found in the devtools on Chrome or Firebug on Firefox. This will allow you to select the image element that you want to drop on the page.
Once you have the element, you can do something like this: from selenium import webdriver. From selenium.webdriver.common.by import from selenium.support.ui import WebDriverWait
From selenium.support import expectedconditions as EC import time. Def main(): driver = webdriver.Chrome('C:/Users/username/chromedriver') driver.get(') wait = WebDriverWait(driver, 30). # Get element that will be dropped. image = driver.findelementbycssselector("div#search-main-content-0 div.c5")
# Select element to drop. selectimage = driver.findelementbycssselector("div#c8") selectimage.click() # Drag the element to position. d = driver.findelementbycssselector("div#c8 div.u0")
Related Answers
How can we use the Selenium tool with HeadSpin?
Selenium is a tool that is used to automate functional testing. There are two types...
What are 5 Uses of Selenium?
Selenium is a web-automation tool that helps you to test web applications....