当目标元素和目标元素在不同的帧中时,如何使用selenium-webdriver执行拖放操作?

时间:2012-08-10 07:41:35

标签: selenium selenium-webdriver automated-tests cq5

我必须拖动图像并将其放入CQ5组件中。图像和组件位于不同的框架中。

以下是无效的代码,因为当目标的帧处于活动状态时无法找到webelement destination

new Actions(driver).dragAndDrop(target, destination).perform();

我还尝试在动作之间切换帧:

    Actions builder = new Actions(driver);
    Actions action = builder.clickAndHold(target);
    driver.switchTo().frame("newFrame"); //switching frames
    builder.moveToElement(destination);
    builder.release(destination);
    builder.build();
    action.perform();

这也不起作用。然后,我尝试按偏移

移动图像
new Actions(driver).dragAndDropBy(target,  x, y).perform(); // x and y 

这会移动图像,但组件没有捕获它,可能因为动作太快了。有没有办法可以做这样的拖拽?

提前致谢。

11 个答案:

答案 0 :(得分:6)

您需要将其分为两部分。

// grab your element
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(target);
builder.build();
action.perform();

// switch to the frame (you havent told webdriver to un-grab
driver.switchTo().frame("newFrame"); //switching frames

// move and drop
Actions builder = new Actions(driver);
Actions action = builder.moveToElement(destination);
builder.release(destination);
builder.build();
action.perform();

答案 1 :(得分:1)

似乎selenium / webdriver拖放存在一些问题。我已经向硒人提交了一个缺陷http://code.google.com/p/selenium/issues/detail?id=4420

希望我们能得到一些积极的回应。

答案 2 :(得分:1)

有人找到了Adobe CQ 5.5的解决方案吗?

我面临与adobe CQ 5.5相同的问题,我正在尝试多种不同的方式,我可以将图像放到区域,但是一旦它在那里图像仍然看起来不活跃并且放下它没有意义。我发现这是因为鼠标指针没有移动图像,这就是为什么掉线没有意义。我添加了代码将鼠标移动到放置区域,但看起来命令是单独工作的,所以仍然无法丢弃,请任何建议。

这是我的代码(不适用于CQ 5.5)

String handle = driver.getWindowHandle(); // for main window

//切换到窗口以便能够选择图像

driver.switchTo().window(handle);
WebElement dragble = driver.findElement(By.xpath("//xpath"));

Actions builder = new Actions(driver);
builder.clickAndHold(dragble);
Action action2 = builder.build();
action2.perform();

//然后,切换到iframe

driver.switchTo().frame("cq-cf-frame");
WebElement droppable = driver.findElement(By.cssSelector("#cssSelector of droppable"));

//将鼠标指向可放置区域的机器人

Point coordinates = driver.findElement(By.cssSelector("#cssSelector of droppable")).getLocation();
Robot robot = new Robot();

//查找droppable元素的位置

int x = driver.findElement(By.cssSelector("#ext-comp-1271")).getLocation().getX();
int y = driver.findElement(By.cssSelector("#ext-comp-1271")).getLocation().getY();

//将dragble移动到droppable

builder = new Actions(driver);
builder.moveByOffset(x,y).perform().
builder.build();
builder.release();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
builder.release(droppable).perform();

答案 3 :(得分:1)

我和你一样。我不能将两个元素从一个框架中丢弃到另一个框架中。 激光器的鞋面是正确的,但从硒3来看,这种解决方案不再适用。 工作是将源元素(在clickAndHol之后)移动到位置0,0然后在第二帧下移动它。例如150,150。

 Actions builder = new Actions(driver);
 // switsh to the source frame
 driver.switchTo().frame("sourceFrame");
 // take the element with mouse 
 builder.clickAndHold(sourceElt).build().perfom();
 // move mouse to the top of the source frame
 builder.moveToElement(sourceElt, 0, 0 ).build().perfom();
 // move the mouse under the target frame (specific to your case)
 builder.moveToElement(sourceElt, 150,200).build().perfom();
 // switsh to the target frame
 driver.switchTo().frame("targetFrame");
 builder.moveToElement(targetElt).build().perform();
 builder.release(target).build().perform();      
希望我能帮助你。

答案 4 :(得分:0)

String source = "xpath_of_source";
String destination = "xpath_of_destination";

// grab your element
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(driver.findElement(By.xpath(source)));
builder.build();
action.perform();

// switch to the frame
driver.switchTo().frame("newFrame"); //switching frames

// move and drop
builder = new Actions(driver);
action = builder.moveToElement(driver.findElement(By.xpath(destination)));
builder.release(driver.findElement(By.xpath(destination)));
builder.build();
action.perform();

答案 5 :(得分:0)

此代码适用于CQ 5.5

driver.switchTo().defaultContent();

Actions builder = new Actions(driver);
builder.clickAndHold(target);
Action action = builder.build();
action.perform();

driver.switchTo().frame("cq-cf-frame");
builder.moveToElement(destination);
builder.release(destination);     
action = builder.build();
action.perform();

答案 6 :(得分:0)

根据CQ 5.5和CQ 5.6,以上发布的解决方案对我不起作用

这有效:

    Actions builder = new Actions(driver);
    builder.clickAndHold(sideKickComponent);
    Action action = builder.build();
    action.perform();

    driver.switchTo().frame("cq-cf-frame");
    builder = new Actions(driver);
    builder.moveToElement(destination).perform();
    builder.build();
    builder.release();
    builder.release(destination).perform();     

此方法可以方便地放置元件:

public void addComponentByDragAndDrop(String sideKickComponentName, WebElement destination){
        driver.switchTo().defaultContent();
        WebElement sidekick = driver.findElement(By.id("cq-sk"));
        List<WebElement> components =sidekick.findElements(By.tagName("button"));
        WebElement sideKickComponent = null;
        for (WebElement webElement : components) {
            if (webElement.getText().equals(sideKickComponentName)) {
                sideKickComponent = webElement;
                break;
            }
        }
        if (sideKickComponent == null) {
            fail("SideKick component with the name: "+sideKickComponentName + " was not found.");
        }
        Actions builder = new Actions(driver);
        builder.clickAndHold(sideKickComponent);
        Action action = builder.build();
        action.perform();

        driver.switchTo().frame(Consts.CQ_MAIN_FRAME);
        builder = new Actions(driver);
        builder.moveToElement(destination).perform();
        builder.build();
        builder.release();
        builder.release(destination).perform();     
    }

答案 7 :(得分:0)

以下代码有效,希望有所帮助:

WebElement dragElement = (WebElement) elements.get(sourceElement);
Actions builder = new Actions(driver);          
Actions action = builder.clickAndHold(dragElement);
action.build().perform();
driver.switchTo().frame("cq-cf-frame");   
WebElement dropElement = driver.findElement(By.id("ext-comp-1411"));
builder.moveToElement(dropElement).build().perform();
//click the destination 
builder.click(dropElement).build().perform();
//back to main page to release the hold mouse
driver.switchTo().defaultContent();
builder.release(dragElement).build().perform();

答案 8 :(得分:0)

要从iframe拖放到另一个iframe,您需要在源Web元素的iframe中引用所有操作。为此,您应该获取目标iframe的父级并使用它进行操作,即CqFrameParent,它是具有目标iframe的div。

由于源和目标属于单个iframe,因此无需切换iframe来执行此操作。

builder.moveElement(CqFrameParent(), targetX, targetY).build().perform();
builder.release().build().perform();

答案 9 :(得分:0)

创建动作类的对象

Actions act=new Actions(driver);

找到我们需要拖动的元素xpath

WebElement drag=driver.findElement(By.xpath("put x path"));

查找我们需要删除的元素xpath

WebElement drop=driver.findElement(By.xpath("put x path"));

将元素拖到目标

act.dragAndDrop(drag, drop).build().perform();

要在CQ中使用拖放,请先使用双击功能然后放置任何组件,然后尝试上述方法。

答案 10 :(得分:-1)

Selenium webdriver提供拖放功能。试试这个

WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();