问题 Selenium 2.0 WebDriver和:hover伪类的解决方法


有没有人可以提供一个c#示例,说明如何通过涉及css伪类的已知Selenium问题:hover?

从本质上讲,我正在开始使用selenium IDE进行回归测试(并在Visual Studio 2008中构建我的其余代码),并且需要将鼠标悬停在div上,使其显示,然后单击div中的链接。

然而,我所有的努力都失败了,似乎许多人都有这个问题,没有解决方案。

提前致谢!


7137
2017-11-29 14:00


起源

你看过这个: stackoverflow.com/questions/2973145/selenium-and-hover-css - prestomanifesto


答案:


好的!所以我很感激帮助(我实际上看过那个帖子,但是.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;

希望这可以帮助!


13
2017-11-30 15:40



非常感谢您提供“使用”声明。没有它,我一直很难在IE上使用W​​ebdriver(C#)搜索鼠标悬停的解决方案。
只是我遇到的问题。谢谢你。 - gottlieb76