Selenium Webdriver Learning (10)------------How to drag and drop one element into another (go)

Source: Internet
Author: User

Selenium Webdriver Learning (10)------------How to drag and drop an element into another element Blog Category:
    • Selenium-webdriver
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
  1. Import Org.openqa.selenium.By;
  2. Import Org.openqa.selenium.WebDriver;
  3. Import org.openqa.selenium.WebElement;
  4. Import Org.openqa.selenium.firefox.FirefoxDriver;
  5. Import org.openqa.selenium.interactions.Actions;
  6. Public class Draganddrop {
  7. /** 
  8. * @author GONGJF
  9. */
  10. public static void Main (string[] args) {
  11. //TODO auto-generated method stub
  12. System.setproperty ("Webdriver.firefox.bin","D:\\Program files\\mozilla firefox\\firefox.exe");
  13. Webdriver dr = new Firefoxdriver ();
  14. Dr.get ("http://koyoz.com/demo/html/drag-drop/drag-drop.html");
  15. //First new out the page element object and target object to be dragged in, then drag in.
  16. webelement element = Dr.findelement (By.id ("item1"));
  17. Webelement target = dr.findelement (By.id ("Drop"));
  18. (new Actions (DR)). Draganddrop (element, target). Perform ();
  19. //Use loops to drag other item into
  20. String id="item";
  21. For (int i=2;i<=6;i++) {
  22. String item = id+i;
  23. (new Actions (DR)). Draganddrop (Dr.findelement (By.id (item)), target). Perform ();
  24. }
  25. }
  26. }
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.