14

我必须将图像拖放到 CQ5 组件中。图像和组件位于不同的帧中。

这是在destination目标框架处于活动状态时无法找到 webelement 的代码。

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 

这移动了图像,但组件没有捕获它,可能是因为动作太快了。有什么办法可以做到这样的拖放吗?

提前致谢。

4

11 回答 11

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();
于 2012-08-13T11:26:50.073 回答
1

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

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

于 2012-08-15T00:05:23.130 回答
1

有人找到 Adob​​e 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();

// 查找可放置元素的位置

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

// 将可拖动对象移动到可放置对象

builder = new Actions(driver);
builder.moveByOffset(x,y).perform().
builder.build();
builder.release();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
builder.release(droppable).perform();
于 2013-06-28T18:44:32.090 回答
1

我和你有同样的prb。我不能将两个元素从一帧拖放到花药。ansers 鞋面是正确的,但从 selenium 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();      

希望我也能帮助你。

于 2016-11-24T14:34:16.270 回答
0

This code works on 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();
于 2012-12-18T09:22:55.167 回答
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();
于 2013-06-20T21:56:25.853 回答
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();     
    }
于 2013-05-23T12:54:04.373 回答
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();
于 2012-11-27T08:09:40.527 回答
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 中使用拖放功能,请先使用双击功能并放置任何组件,然后尝试上述方法。

于 2017-08-16T15:28:43.470 回答
0

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

由于源和目标属于单个 iframe,因此无需进行 iframe 的切换即可。 

builder.moveElement(CqFrameParent(), targetX, targetY).build().perform();
builder.release().build().perform();
于 2015-08-03T04:29:42.617 回答
-1

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

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

(new Actions(driver)).dragAndDrop(element, target).perform(); 
于 2012-11-30T12:57:34.383 回答