无法将元素拖放到另一个元素,Selenium 版本 3.9.1,chromedriver 2.36
Not able to drag and drop an element to another element , Selenium version 3.9.1, chromedriver 2.36
Url : http://www.seleniumeasy.com/test/drag-and-drop-demo.html
操作:我正在尝试将 Draggable1 拖到下拉框
让我知道哪里出了问题,因为我已经尝试了每个论坛上提供的所有内容。
如何实现?
图片
代码:
driver.get("http://www.seleniumeasy.com/test/");
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("arguments[0].click()",
driver.findElement(By.xpath("//*[@id='navbar-brand-centered']/ul[2]/li[4]/ul/li[1]/a")));
Actions actions = new Actions(driver);
WebElement sourceElement = driver.findElement(By.xpath("//*[@id='todrag']/span[1]"));
int sourceElementWidth = sourceElement.getLocation().getX() + sourceElement.getSize().getWidth() / 2;
int sourceElementHeight = sourceElement.getLocation().getY() + sourceElement.getSize().getHeight() / 2;
System.out.println(sourceElementWidth + "," + sourceElementHeight);
WebElement destinationElement = driver.findElement(By.xpath("//*[@id='mydropzone']"));
int destinationElementWidth = destinationElement.getLocation().getX()
+ destinationElement.getSize().getWidth() / 2;
int destinationElementHeight = destinationElement.getLocation().getY()
+ destinationElement.getSize().getHeight() / 2;
System.out.println(destinationElementWidth + "," + destinationElementHeight);
Action action = actions.pause(2000).moveToElement(sourceElement).moveByOffset(25, 10).click().clickAndHold()
.pause(1000).moveToElement(destinationElement, destinationElement.getSize().getWidth() / 2,
destinationElement.getSize().getHeight() / 2)
.release().pause(2000).build();
action.perform();
driver.quit();
试试下面的代码:
WebElement LocatorFrom=driver.findElement(<locator of from where element need to be dragged>);
//Element on which need to drop.
WebElement LocatorTo=driver.findElement(<locator where element need to be dropped>);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("function createEvent(typeOfEvent) {\n" +"var event =document.createEvent(\"CustomEvent\");\n" +"event.initCustomEvent(typeOfEvent,true, true, null);\n" +"event.dataTransfer = {\n" +"data: {},\n" +"setData: function (key, value) {\n" +"this.data[key] = value;\n" +"},\n" +"getData: function (key) {\n" +"return this.data[key];\n" +"}\n" +"};\n" +"return event;\n" +"}\n" +"\n" +"function dispatchEvent(element, event,transferData) {\n" +"if (transferData !== undefined) {\n" +"event.dataTransfer = transferData;\n" +"}\n" +"if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n" +"} else if (element.fireEvent) {\n" +"element.fireEvent(\"on\" + event.type, event);\n" +"}\n" +"}\n" +"\n" +"function simulateHTML5DragAndDrop(element, destination) {\n" +"var dragStartEvent =createEvent('dragstart');\n" +"dispatchEvent(element, dragStartEvent);\n" +"var dropEvent = createEvent('drop');\n" +"dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n" +"var dragEndEvent = createEvent('dragend');\n" +"dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" +"}\n" +"\n" +"var source = arguments[0];\n" +"var destination = arguments[1];\n" +"simulateHTML5DragAndDrop(source,destination);",LocatorFrom, LocatorTo);
Thread.sleep(1500);
Url : http://www.seleniumeasy.com/test/drag-and-drop-demo.html
操作:我正在尝试将 Draggable1 拖到下拉框 让我知道哪里出了问题,因为我已经尝试了每个论坛上提供的所有内容。 如何实现?
图片
代码:
driver.get("http://www.seleniumeasy.com/test/");
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("arguments[0].click()",
driver.findElement(By.xpath("//*[@id='navbar-brand-centered']/ul[2]/li[4]/ul/li[1]/a")));
Actions actions = new Actions(driver);
WebElement sourceElement = driver.findElement(By.xpath("//*[@id='todrag']/span[1]"));
int sourceElementWidth = sourceElement.getLocation().getX() + sourceElement.getSize().getWidth() / 2;
int sourceElementHeight = sourceElement.getLocation().getY() + sourceElement.getSize().getHeight() / 2;
System.out.println(sourceElementWidth + "," + sourceElementHeight);
WebElement destinationElement = driver.findElement(By.xpath("//*[@id='mydropzone']"));
int destinationElementWidth = destinationElement.getLocation().getX()
+ destinationElement.getSize().getWidth() / 2;
int destinationElementHeight = destinationElement.getLocation().getY()
+ destinationElement.getSize().getHeight() / 2;
System.out.println(destinationElementWidth + "," + destinationElementHeight);
Action action = actions.pause(2000).moveToElement(sourceElement).moveByOffset(25, 10).click().clickAndHold()
.pause(1000).moveToElement(destinationElement, destinationElement.getSize().getWidth() / 2,
destinationElement.getSize().getHeight() / 2)
.release().pause(2000).build();
action.perform();
driver.quit();
试试下面的代码:
WebElement LocatorFrom=driver.findElement(<locator of from where element need to be dragged>);
//Element on which need to drop.
WebElement LocatorTo=driver.findElement(<locator where element need to be dropped>);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("function createEvent(typeOfEvent) {\n" +"var event =document.createEvent(\"CustomEvent\");\n" +"event.initCustomEvent(typeOfEvent,true, true, null);\n" +"event.dataTransfer = {\n" +"data: {},\n" +"setData: function (key, value) {\n" +"this.data[key] = value;\n" +"},\n" +"getData: function (key) {\n" +"return this.data[key];\n" +"}\n" +"};\n" +"return event;\n" +"}\n" +"\n" +"function dispatchEvent(element, event,transferData) {\n" +"if (transferData !== undefined) {\n" +"event.dataTransfer = transferData;\n" +"}\n" +"if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n" +"} else if (element.fireEvent) {\n" +"element.fireEvent(\"on\" + event.type, event);\n" +"}\n" +"}\n" +"\n" +"function simulateHTML5DragAndDrop(element, destination) {\n" +"var dragStartEvent =createEvent('dragstart');\n" +"dispatchEvent(element, dragStartEvent);\n" +"var dropEvent = createEvent('drop');\n" +"dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n" +"var dragEndEvent = createEvent('dragend');\n" +"dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" +"}\n" +"\n" +"var source = arguments[0];\n" +"var destination = arguments[1];\n" +"simulateHTML5DragAndDrop(source,destination);",LocatorFrom, LocatorTo);
Thread.sleep(1500);