在 C# 中使用 Selenium Webdriver,我需要将鼠标悬停在一个元素上,然后该元素会显示隐藏元素的菜单,然后我需要单击之前隐藏的元素。我无法单击显示菜单的元素,因为这将重定向到另一个页面。
行动:
- 将鼠标悬停在“我的帐户”菜单项上可显示隐藏元素(不得点击此元素)
- 找到并点击第 1 步中显示的“更新详细信息”元素。
但是,我正在使用以下代码;它引发“更新详细信息”元素的元素未找到异常。
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();
最佳答案
您只需要操作即可进行悬停。将鼠标悬停后,您应该能够单击该元素。我在那里等待,以确保在您尝试单击该元素之前,页面有一秒钟的时间让该元素可见。您可能不需要它。
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();
关于c# - 将鼠标悬停在元素上以显示并单击隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650491/