2011-11-29 38 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