我在下面编写了代码来拖动元素并将其添加到工作区中。控制台窗口中没有错误,但是在chrome浏览器上没有执行drap drop。
WebElement dragElement = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement dropElement = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);
act.clickAndHold(dragElement).moveToElement(dropElement).release().build().perform();
我已多次尝试但未能成功。请提供您的意见
答案 0 :(得分:0)
您可以尝试提供元素的位置
npm install --save-dev sw-offline-google-analytics
...
importScripts('path/to/offline-google-analytics-import.js');
goog.offlineGoogleAnalytics.initialize();
答案 1 :(得分:0)
这是Selenium文档中提供的另一种方法:http://www.seleniumhq.org/docs/03_webdriver.jsp#drag-and-drop。
WebElement dragElement = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
WebElement dropElement = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
(new Actions(driver)).dragAndDrop(dragElement, dropElement).perform();
答案 2 :(得分:0)
你可以使用下面的代码进行拖放,但我建议你优化你的xpath。这可能是你真正的问题。
WebElement source = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement target = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);
Actions builder = new Actions(driver);
Action mouseOverHome = builder.dragAndDrop(source, target).build();
mouseOverHome.perform();
答案 3 :(得分:0)
我在Firefox中遇到了类似的问题,并通过在moveToElement()指令前添加额外的移动指令来解决它,如下所示:
private void dragAndDrop(WebElement element, WebElement target) {
Actions builder = new Actions(driver);
builder.clickAndHold(element);
builder.moveByOffset(20,20); // THIS was the critical part for me
builder.moveToElement(target);
builder.release();
builder.perform();
}