从iframe拖动元素并放入另一个iframe

时间:2017-01-31 22:33:45

标签: selenium iframe selenium-webdriver alert

我有两个iframe。我必须从iframe拖动图像并将图像放在另一个iframe中。我尝试了以下代码并且无法正常工作

Actions builder = new Actions(driver);
builder.moveToElement(fromElement);
builder.clickAndHold(fromElement);
builder.build();
builder.perform();
driver.switchTo().defaultContent();
WebElement pageFrame = driver.findElement(By.cssSelector("iframe[src='components/svg-edit-2.6-unmodified/svg-editor.html']"));
driver.switchTo().frame(pageFrame);
WebElement rightPanel = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
builder.moveToElement(rightPanel);
builder.release(rightPanel);
builder.build();
builder.perform();

还尝试使用坐标移动元素,但这对我也不起作用。
action.clickAndHold(fromElement).moveByOffset(25, 0).release().perform();
任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试下面提到的代码。

driver.switchTo().frame(0);                                 //Move inside to the frame. 
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//your xpath")); 
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(4000);
driver.switchTo().defaultContent();                        //Move outside to the frame.

driver.switchTo().frame(1);                               //Move inside to another frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement to = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent();                      //Move outside to another frame.