带有Java的Selenium Web驱动程序中的拖放元素问题

时间:2013-08-05 13:41:48

标签: selenium webdriver

在左侧窗格中有一些小部件,下面的代码给出了小部件如何组织的一些想法....

<div id="sites-widgets-1375699547529" class="sites-widget-container tab-content-div frog-touch-scroll active">
<div class="ui-editor-box">
<div class="image draggable" data-content="file/widget/0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA/icon.png" data-uuid="0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA" style="background-image:url(file/widget/0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA/icon.png);"/>
<div class="ui-editor-box-label">Text Activity</div>
</div>
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
</div>

我想将窗口小部件拖放到同一网页中提供的容器中。容器的代码如下所示

<div class="sites-layout-one-one">
<div style="clear:both;"/>
<div class="bucket ui-sortable edit" data-order="1" data-attr="bucket"/>
<div style="clear:both;"/>
</div>

我正在尝试将小部件拖放到容器中,但我无法做到这一点,我写的代码在下面

WebElement dragElement = driver.findElement(By.xpath("//div[@class='sites-widget-container tab-content-div frog-touch-scroll active']/div[@class='ui-editor-box'][1]"));

WebElement dropElement = driver.findElement(By.xpath("//div[@class='sites-layout-one-one']"));

Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(dragElement)
                    .moveToElement(dropElement)
                    .release(dropElement)
                    .build();
                      dragAndDrop.perform();
        Thread.sleep(10000);

请帮我解决这个问题......

先谢谢

希瓦..

1 个答案:

答案 0 :(得分:1)

只需使用内置的dragAndDrop操作即可为您处理:

Actions builder = new Actions(driver);
builder.dragAndDrop(dragElement, dropElement).perform();

API文档位于此处,显示了dragAndDrop操作的用法:

  

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html