2014-12-08 40 views
2

我正在做一個執行拖放操作的測試。如何正確模擬硒中的拖放

我當前的代碼:

WebElement element; 
By mainSelector, secondarySelector; 
Actions action; 

action = new Actions(driver); 

mainSelector = By.cssSelector("tbody.naam tr:nth-child(1) td:nth-child(1)"); 
secondarySelector = By.cssSelector("tbody.bedrijf tr:nth-child(1) td:nth-child(1)"); 

action.click(driver.findElement(mainSelector)); 

action.clickAndHold(driver.findElement(mainSelector)) 
    .moveToElement(driver.findElement(secondarySelector), 5, 5) 
    .perform(); 
action.release(driver.findElement(secondarySelector)); 

action.perform(); 

action.dragAndDropBy(driver.findElement(mainSelector), 300, 300).perform(); 

action.dragAndDrop(driver.findElement(mainSelector), driver.findElement(secondarySelector)).perform(); 

但是,這並不做任何事情。 我已經添加了多個執行,因此請確保這不是問題。 我已經添加了一個偏移量,因爲我讀到這有時是越野車。 我用firefox進行測試。

+0

請檢查:http://stackoverflow.com/questions/14210051/how-to-automate-drag-drop-functionality-using-selenium-web-driver – 2014-12-08 09:09:32

回答

-1

如果你想拖mainSelector到secondarySelector,你可以做這樣的事情

Method 1

mainSelector = driver.findElement(By.cssSelector("tbody.naam tr:nth-child(1) td:nth-child(1)")); 
secondarySelector = driver.findElement(By.cssSelector("tbody.bedrijf tr:nth-child(1) td:nth-child(1)")); 

action = new Actions(driver) 
action.dragAndDrop(mainSelector, secondarySelector).perform(); 

Method 2

action.clickAndHold(mainSelector).moveToElement(secondarySelector).release().build().perform(); 

兩種方法將做任務。

希望它有幫助! :)

+0

'dragAndDrop'函數只接受WebElements的權利?不是'選擇器'? – Funonly 2014-12-08 09:23:40

+0

是啊dragAndDrop接受WebElements,我已經編輯回答。 – Paras 2014-12-08 09:29:38

+0

我已經將它添加到嘗試列表中,但不幸的是,它在這裏並不適合我。 – Funonly 2014-12-08 09:35:22