2014-12-07 17 views
0

我目前正在閱讀一本書來學習硒,我無法獲得一些正確運行的代碼示例。 下面的代碼應該點擊3個貼圖,但它只能點擊前2個。 。 。硒'moveByOffset'將只能工作2次

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.*; 

public class MoveByOffSetAndClick { 
    public static void main(String... args){ 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("file:///C:/Selectable.html"); 
     WebElement one = driver.findElement(By.name("one")); 
     WebElement eleven = driver.findElement(By.name("eleven")); 
     WebElement six = driver.findElement(By.name("six")); 

     int borderWidth = 1;   
     Actions builder = new Actions(driver); 

     builder.moveByOffset(one.getLocation().getX() + borderWidth, 
       one.getLocation().getY() + borderWidth).click(); 
     builder.build().perform(); 

     builder.moveByOffset(six.getLocation().getX() + borderWidth, 
       six.getLocation().getY() + borderWidth).click();  
     builder.build().perform(); 

     builder.moveByOffset(eleven.getLocation().getX() + borderWidth, 
       eleven.getLocation().getY() + borderWidth).click(); 
     builder.build().perform(); 

     driver.quit(); 

    } 
} 

有人對此有何看法?

操作系統:Windows 7專業版64位 的Java:7u71 64 的Eclipse:月神服務版本1(4.4.1)64位

Here'sthe HTML它正在...

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>jQuery UI Selectable - Display as grid</title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
<style> 
#feedback { font-size: 1.4em; } 
#selectable .ui-selecting { background: #FECA40; } 
#selectable .ui-selected { background: #F39814; color: white; } 
#selectable { list-style-type: none; margin: 0; padding: 0; width: 450px; } 
#selectable li { float: left; width: 100px; height: 80px; font-size: 4em; text-align: center; } 
</style> 
<script> 
$(function() { 
$("#selectable").selectable(); 
}); 
</script> 
</head> 
<body> 
<ol id="selectable"> 
<li class="ui-state-default" name="one">1</li> 
<li class="ui-state-default" name="two">2</li> 
<li class="ui-state-default" name="three">3</li> 
<li class="ui-state-default" name="four">4</li> 
<li class="ui-state-default" name="five">5</li> 
<li class="ui-state-default" name="six">6</li> 
<li class="ui-state-default" name="seven">7</li> 
<li class="ui-state-default" name="eight">8</li> 
<li class="ui-state-default" name="nine">9</li> 
<li class="ui-state-default" name="ten">10</li> 
<li class="ui-state-default" name="eleven">11</li> 
<li class="ui-state-default" name="twelve">12</li> 
</ol> 
</body> 
</html> 
+0

您能否告訴我們在執行上面的代碼時會得到什麼異常? – Subh 2014-12-07 12:28:38

+0

嗨。我沒有得到任何例外。代碼只是點擊前2個瓷磚,然後完成:( – Paddykool 2014-12-07 15:32:28

+0

您的代碼應該工作,我沒有看到任何問題,你可以在** Selectable.html **中添加HTML代碼片段嗎?我的結尾,並檢查異常。謝謝。 – Subh 2014-12-07 15:37:17

回答

2

使用的是永遠不能工作,你打算使用它的方式的做法。documentation for moveByOffset說:

從當前位置(或0,0)移動鼠標由給定的偏移

即,通過偏移相對於當前鼠標位置移動鼠標。在您的代碼中,您可以使用相對於文檔的座標來調用它。這是第一次運作,因爲如果尚未建立鼠標位置,則初始鼠標座標爲0, 0。它也是第二次工作,因爲當你打第二個電話時,鼠標位於第一個列表項內,因此仍然接近0, 0。當你點擊,鼠標點擊是不會發生,你意味着它,但它是仍然six,所以它不是足夠使你的代碼小姐six。但是,在第三次通話時,通話前的鼠標座標位於six。這樣第三次撥打電話moveByOffset會將鼠標移出eleven以外。

這是真的錯誤或沒有錯誤。

通常,單擊元素內部時不需要擔心邊框。 click()方法將自動將鼠標移動到您關心的元素的中心點,因此通常無需您調整鼠標位置即可正常工作。在特殊情況下,您可能需要擔心偏移量,但是您尚未顯示出需要擔心的情​​況。

+0

很好的解釋。非常感謝你們兩位 – Paddykool 2014-12-08 13:58:25

0

Yah Subh,

Yah - 我用下面的代碼來解決它。非常感謝您的意見!

WebDriver driver = new FirefoxDriver(); 
driver.get("file:///C:/Selectable.html"); 
WebElement one = driver.findElement(By.name("one")); 
WebElement eleven = driver.findElement(By.name("eleven")); 
WebElement six = driver.findElement(By.name("six")); 

Actions builder = new Actions(driver); 

builder.click(one).click(six).click(eleven); 
builder.build().perform();