当目标元素和目标元素在不同的帧中时,如何使用 selenium-webdriver 执行拖放? [英] How to perform drag and drop using selenium-webdriver when target and destination element are in different frames?

查看:14
本文介绍了当目标元素和目标元素在不同的帧中时,如何使用 selenium-webdriver 执行拖放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将图像拖放到 CQ5 组件中.图像和组件在不同的框架中.

I have to drag an image and drop it into a CQ5 component. The image and component are in different frames.

这是在目标框架处于活动状态时无法作为 webelement destination工作的代码.

Here is the code which did not work as webelement destinationcould not be found when the target's frame was active.

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

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

I have also tried to switch frame in between action as:

    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();

这也不起作用.然后,我尝试通过偏移量移动图像

This is did not work either. Then, I tried moving the image by offset

new Actions(driver).dragAndDropBy(target,  x, y).perform(); // x and y 

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

This moved the image but component did not capture it, probably becuase action was too fast. Is there any way such drag drop can be done?

提前致谢.

推荐答案

你需要把它分成两部分.

You need to break it into two parts.

// 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();

这篇关于当目标元素和目标元素在不同的帧中时,如何使用 selenium-webdriver 执行拖放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆