2014-03-07 95 views
0

我試圖搜索谷歌圖像中的文本,然後拖動元素並放下。然而,它不是一樣的。當我們手動移動鼠標時,一些動作正在發生,但是拖放功能沒有執行。無法執行拖放式Webdriver Java

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

回答

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 
+0

我已經嘗試過兩種方法,但仍然以相同的方式工作。手動移動鼠標時,會顯示一些拖動操作,但不會執行拖放操作。 – Dinu

0

碼本元素拖放到一個偏移

WebElement拖動= browser.findElement(By.id( 「拖動」 ));新的 操作(瀏覽器).dragAndDropBy(可拖動,200,10).build().execute();