2011-11-29 33 views
11

有沒有人可以提供一個c#示例如何克服涉及css僞類的已知Selenium問題:hover?從本質上講,我正在爲一個網站startin w/selenium IDE進行迴歸測試(並在Visual Studio 2008中構建我的代碼的其餘部分),並且需要將鼠標懸停在div上,使其顯示,然後單擊鏈接裏面說div。Selenium 2.0 WebDriver&的一個解決方法:懸停僞類

但是我所有的努力都失敗了,似乎很多人都有這個問題,沒有解決方案。

在此先感謝!

+0

你看這個:!http://stackoverflow.com/questions/2973145/selenium-and- hover-css – prestomanifesto

回答

13

好吧!所以,我欣賞的幫助(我實際上看到線程,但.hover()類已棄用,但我無法得到它的工作,我沒有,但是,只要找到一個堅實的解決辦法。

var phone = driver.FindElement(By.Id("phones")); 
var phoneLi = phone.FindElements(By.TagName("li")); 
Actions action = new Actions(driver);//simply my webdriver 
action.MoveToElement(phoneLi[1]).Perform();//move to list element that needs to be hovered 
var click = action.MoveToElement(phoneLi[1].FindElements(By.TagName("a"))[0];//move to actual button link after the 'Li' was hovered 
click.Click(); 
click.Perform(); //not too sure why I needed to use both of these, but I did. Don't care, it works ;) 
IAlert alert = driver.SwitchTo().Alert(); 
alert.Accept(); 

此外,您需要使用包含語句,有一對夫婦

using OpenQA.Selenium; 
using OpenQA.Selenium.Interactions; 
using OpenQA.Selenium.Interactions.Internal; 
using OpenQA.Selenium.Support.UI; 

希望這有助於

+2

非常感謝包括'使用'的陳述。沒有它,我一直有一個艱難的時間t試圖在IE上使用W​​ebdriver(C#)搜索鼠標懸停的解決方案。 – 2012-08-28 15:33:16

+0

只是我有問題。謝謝你。 – gottlieb76