无法执行Drag-Drop-Webdriver Java

时间:2014-03-07 06:07:16

标签: java testing selenium automation selenium-webdriver

我尝试在Google图片中搜索文字,然后拖动元素并放下。但是它的表现并不相同。当我们手动移动鼠标时,会发生一些操作但不执行拖放功能。

WebDriver driver= new FirefoxDriver();
    driver.get("http://images.google.com/");
    Thread.sleep(1000);
    driver.findElement(By.name("q")).sendKeys("Apple");
    driver.findElement(By.name("btnG")).click();
    WebElement drag= driver.findElement(By.cssSelector("html body#gsr.srp div#main div#cnt.mdm div#rcnt div.col div#center_col div#res.med div#topstuff div#ifbc.prc div#ifb.prs a.rg_fbl div.rg_bb div.rg_bb_i div.rg_bb_layout div.rg_di img.rg_i"));
    WebElement drop= driver.findElement(By.cssSelector("html body#gsr.srp div#tphdr.tphdr div#mngb div#gb.gb_1b div.gb_Ab div.gb_k div#gbq div#gbq2.gbt div#gbqfw.gbqfr form#gbqf.gb_Nb fieldset#gbqff.gbqff div#gbfwa.gbqfwa div#gbqfqw.gbqfqw div#gbqfqwb.gbqfqwc table#gs_id0.gstl_0 tbody tr td#gs_tti0.gsib_a div#gs_lc0 input#gs_htif0.gbqfif"));
    Actions builder= new Actions(driver);
    Action dragelement= builder.clickAndHold(drag).build();
    dragelement.perform();
    Action dropelement=builder.moveToElement(drop).release(drop).build();
    dropelement.perform();

2 个答案:

答案 0 :(得分:0)

试试这个...

WebDriver driver = new FirefoxDriver();    
driver.get("http://images.google.com/");  

driver.switchTo().frame(0);   

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);    

WebElement dragElement = driver.findElement(By.id("draggable"));  

WebElement dropElement = driver.findElement(By.id("droppable")); 

Actions builder = new Actions(driver);  // Configure the Action  

Action dragAndDrop = builder.clickAndHold(dragElement)
                            .moveToElement(dropElement)
                            .release(dropElement).build();  // Get the action  

dragAndDrop.perform(); // Execute the Action

答案 1 :(得分:0)

将此元素拖动到偏移量的代码是

  

WebElement draggable = browser.findElement(By.id(“draggable”));新   动作(浏览器).dragAndDropBy(draggable,200,10).build()。perform();