如何避免Selenium上的MouseOver Click()

在我的一个Selenium测试用例中,我遇到了一个问题,即我不想拥有MouseOver效果。 这就是我做的:

  1. 点击“登录”按钮(页面右上角)
  2. 等待页面加载
  3. 单击搜索结果中的“购买”按钮(页面中间右侧)。

问题是,在“登录”和“购买”之间的中间有一个带有MouseOver效果的“购物篮”链接。 因此,当我在“登录”按钮上调用Click()并随后在“购买”按钮上调用时,我会触发MouseOver,这会打开购物车的小预览,其中隐藏了“购买”按钮。

这适用于Firefox和MSIE。 在Chrome中,我没有这种效果。

有人在想吗?

安德鲁是对的。 如果没有手动发生,那么它也不应该发生在这里。

您也可以尝试使用JavascriptExecutor单击

 WebElement element= driver.findElement(By.xpath("Your Xpath")); JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript("arguments[0].click();", element); 

下面是一些如何使用C#来做到这一点的例子

在C#中使用Selenium WebDriver执行JavaScript

希望它能帮到你:)

我仍然不知道真正的解决方案,但这是我正在使用的肮脏的解决方法:

 public static void Click(this IWebElement element, TestTarget target) { if (target.IsInternetExplorer) { var actions = new Actions(target.Driver); actions.MoveToElement(element).Perform(); Thread.Sleep(500); // wait for the mouseover popup to appear element.SendKeys(Keys.Escape); // to close the popup (if any) actions.MoveToElement(element).DoubleClick().Perform(); // simple click is sometimes not enough in IE } else { element.Click(); } }