2013-03-09 61 views
0

我寫了下面的代碼,但沒有找到元素。該網站的鏈接是http://plugnedit.com/在Selenium WebDriver中拖放操作的問題

我已經嘗試過各種方法來定位頁面上的元素來嘗試拖動對象。它不工作。獲取「無法定位元素」錯誤。

我正在使用firefox。

謝謝。

 driver.navigate().to("http://plugnedit.com/"); 
     Actions dragAndDrop = new Actions(driver); 
     WebElement itemToDrag = driver.findElement(By.id("p1003upperspan")); 

     // drag downwards 
     int numberOfPixelsToDragTheScrollbarDown = 10; 
     for (int i=10;i<150;i=i+numberOfPixelsToDragTheScrollbarDown){ 
      // this causes a gradual drag of the scroll bar, 10 units at a time 
      dragAndDrop.moveToElement(itemToDrag).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform(); 
     } 

回答

0

如你正在試圖拖放存在一幀內的元件,則需要先切換到包含該元素的幀,然後執行拖放功能。

從你的代碼中,我能夠弄清楚你試圖拖動的圖像是帶有咖啡杯的圖像。該圖像出現在網頁的第一幀中。

所以我在你的拖放代碼之前添加了下面一行代碼。

driver.switchTo().frame(0); 

//這裏的框架沒有任何名稱。 所以我用索引。這裏frame(0)表示第一幀標籤。

它的工作!使用此代碼。

driver.navigate().to("http://plugnedit.com/"); 

    driver.switchTo().frame(0); 
     Actions dragAndDrop = new Actions(driver); 
     WebElement itemToDrag = driver.findElement(By.id("p1003upperspan")); 

     // drag downwards 
     int numberOfPixelsToDragTheScrollbarDown = 10; 
     for (int i=10;i<150;i=i+numberOfPixelsToDragTheScrollbarDown){ 
      // this causes a gradual drag of the scroll bar, 10 units at a time 
      dragAndDrop.moveToElement(itemToDrag).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform(); 
     }