Selenium Webdriver Learning (10)------------How to drag and drop an element into another element
Blog Category:
Element drag and drop drag and drop
Q Group Sometimes people ask, selenium webdriver How to implement an element to drag and drop into another element. This section is a total of drag and drop elements.
The following page is a page that demonstrates drag-and-drop elements, and you can drag and drop the items from the left and right pages into the Div box on the top.
Http://koyoz.com/demo/html/drag-drop/drag-drop.html
Now let's see how selenium Webdriver realizes drag and drop. Let ' s go!
Java code
- Import Org.openqa.selenium.By;
- Import Org.openqa.selenium.WebDriver;
- Import org.openqa.selenium.WebElement;
- Import Org.openqa.selenium.firefox.FirefoxDriver;
- Import org.openqa.selenium.interactions.Actions;
- Public class Draganddrop {
- /**
- * @author GONGJF
- */
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- System.setproperty ("Webdriver.firefox.bin","D:\\Program files\\mozilla firefox\\firefox.exe");
- Webdriver dr = new Firefoxdriver ();
- Dr.get ("http://koyoz.com/demo/html/drag-drop/drag-drop.html");
- //First new out the page element object and target object to be dragged in, then drag in.
- webelement element = Dr.findelement (By.id ("item1"));
- Webelement target = dr.findelement (By.id ("Drop"));
- (new Actions (DR)). Draganddrop (element, target). Perform ();
- //Use loops to drag other item into
- String id="item";
- For (int i=2;i<=6;i++) {
- String item = id+i;
- (new Actions (DR)). Draganddrop (Dr.findelement (By.id (item)), target). Perform ();
- }
- }
- }
The code is simple and it is important to note that (new Actions (DR)). Draganddrop (element, target). Perform (); In this sentence, Draganddrop (element, target) This method defines the " Click on the element object and hold it until it is dragged into the target element object to release the actions of this series, which will not be performed if you do not call the Perform () method. over!
Selenium Webdriver Learning (10)------------How to drag and drop one element into another (go)