2013-10-29 41 views
5

我有一個HTML div標籤,div內有一個元素出現在鼠標進入其邊界時。現在我想單擊鼠標進入或懸停時可見的元素。使用IE的Selenium Hover元素

問題:元素開始閃爍。 瀏覽器:IE8

我使用下面

IWebElement we = addToBasket.FindElement(By.Id("MyBox")); 
    action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform(); 

任何建議,爲什麼它閃爍的代碼?

+0

您正在使用哪種版本的硒? – Karthikeyan

+0

IE11仍然存在這個問題。幸運的是,下面的解決方案仍然有效:)。 –

回答

17

由於IE驅動程序稱爲「永久懸停」的功能,元素閃爍。此功能具有可疑價值,但因使用SendMessage API而導致的腦死亡方式IE(瀏覽器,而不是驅動程序)responds to WM_MOUSEMOVE messages,因此是必需的。

您有幾個選項。您可以通過使用類似於下面的代碼把持久徘徊關閉:

InternetExplorerOptions options = new InternetExplorerOptions(); 
options.EnablePersistentHover = false; 
IWebDriver driver = new InternetExplorerDriver(options); 

但請注意,這將導致您遭受的其中物理鼠標光標在屏幕上,當你嘗試懸停率性。如果這是不可接受的,你有一個couple of other approaches你可以採取。首先,您可以關閉所謂的「本地事件」,這會導致驅動程序完全依賴合成的JavaScript事件。由於僅依靠JavaScript來合成鼠標事件,因此此方法有其自身的缺陷。

InternetExplorerOptions options = new InternetExplorerOptions(); 
options.EnableNativeEvents = false; 
IWebDriver driver = new InternetExplorerDriver(options); 

最後,你可以使用默認的SendMessage用Windows API代碼,使用更正確的SendInput API遷移。這是通過RequireWindowFocus屬性完成的。它的缺點是鼠標輸入在系統中被注入很低的級別,這要求IE窗口成爲系統的前景窗口。

InternetExplorerOptions options = new InternetExplorerOptions(); 
options.RequireWindowFocus = true; 
IWebDriver driver = new InternetExplorerDriver(options); 

作爲最後一點,不要試圖一次設置所有這些屬性;選擇一種方法並堅持下去。其中有幾個是互斥的,它們之間的交互是不確定的。

+0

謝謝吉姆。使用上面的選項閃爍停止。但後來我不得不像你所說的那樣在物理上懸停鼠標。我找到了適合我的案例的解決方案。 http://code.google.com/p/selenium/wiki/InternetExplorerDriver。他們在運行測試時說,讓鼠標在瀏覽器的邊界內保持不變。停止閃爍,我可以點擊元素。 – Aman

+0

然後隨意標記爲合適的答案。 – JimEvans

+0

從Selenium 2.47.1開始,API已經改變。現在它是ieCapabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS,false);. –

0

這對我有效。

WebElement element = driver.findElement(By.xpath("element xpath")); 
Locatable hoverItem = (Locatable) element; 
Mouse mouse = ((HasInputDevice) driver).getMouse(); 
mouse.mouseMove(hoverItem.getCoordinates()); 
相關問題