将鼠标悬停在元素上以显示并单击隐藏元素

时间:2015-09-18 11:07:17

标签: c# selenium-webdriver

在C#中使用Selenium Webdriver我需要将鼠标悬停在一个元素上,然后显示隐藏元素的菜单,然后我需要单击之前隐藏的元素。我无法单击显示菜单的元素,因为这将重定向到另一个页面。

操作:

  1. 将鼠标悬停在“我的帐户”菜单项上以显示隐藏的元素(不得点击此元素)
  2. 查找并单击由步骤1显示的“更新详细信息”元素。
  3. 然而,我正在使用以下代码;它会引发“更新详细信息”元素的元素未找到异常。

    Actions action = new Actions(driver);
    IWebElement MegaMenu = driver.FindElement(By.CssSelector("#tab-overview"));
    action.MoveToElement(MegaMenu).Build().Perform();
    action.MoveToElement(driver.FindElement(By.LinkText("Update my details"))).Click().Build().Perform();
    

1 个答案:

答案 0 :(得分:2)

您只需要Action来执行悬停。悬停后,您应该只需单击该元素即可。我在那里等待,以确保页面有一秒钟,以便在您尝试单击它之前显示该元素。你可能不需要它。

Actions action = new Actions(driver);
IWebElement MegaMenu = driver.FindElement(By.CssSelector("#tab-overview"));
action.MoveToElement(MegaMenu).Build().Perform();
WebDriverWait Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement updateLink = Wait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Update my details")));
updateLink.Click();