【发布时间】:2020-01-03 17:44:30
【问题描述】:
使用 Visual Studio 和 Selenium,我试图将鼠标悬停在下拉菜单中的某个元素上,以便单击其中的“隐藏”元素。不能只点击,我必须悬停。
我尝试了几种不同的方法都没有成功:
首先,我尝试将鼠标悬停在第一个元素上,如下所示:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Actions builder = new Actions(driver);
IWebElement hoverElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("xpath adress here')]")));
builder.MoveToElement(hoverElement).Build().Perform();
这种方法使第一个下拉元素被“标记”,但由于某种原因下拉菜单没有打开,所以我无法点击第二个元素。
然后我尝试将鼠标悬停在第一个元素上,并在同一命令中单击第二个元素:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Actions builder = new Actions(driver);
IWebElement hoverElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("xpath adress here')]")));
builder.MoveToElement(hoverElement).MoveToElement(wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("second xpath adress")))).Click().Build().Perform();
这根本不起作用。第一个元素甚至没有被“标记”。测试仅在 10 秒后超时。
所以现在我尝试使用定位器:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Actions builder = new Actions(driver);
IWebElement hoverElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("xpath adress for first element')]")));
builder.MoveToElement(hoverElement).Perform();
By locator = By.XPath("2nd element xpath");
driver.click(locator);
但是我不能执行这个测试,因为 IWebDriver 不包含点击的定义。当我尝试写入时出现此错误:
driver.click
有什么建议吗?
第一个元素:
By.XPath("//span[contains(@class,'item-text')][contains(text(),'Change')]"
第二个元素:
By.XPath("//span[contains(@class,'item-text')][contains(text(),'Create Change Order')]"
更新:
悬停下拉菜单部分的 HTML 结构如下所示:
<div class="topbar-menu-dd responsive-dropdown-menu add">
<div class="scrollable-dropdown-menu scrollable-dropdown-menu-root dropdown-
menu dropdown-menu-root dropdown-menu dropdown-menu-root dropdown
dropdown-root responsive-dropdown-menu-root active">
<div class="no-native-scrollbars scroller-content">
<ul class="dropdown-menu-wrap" tabindex="1">
<li class="item topbar-menu-dd-item item-submenu" id="699143f1-
e470-49f0-99f4-a96fee365260/c171" title name="699143f1-e470-49f0-
99f4-a96fee365260/c171">
<div class="item item-template" id="699143f1-e470-49f0-99f4-
a96fee365260/c171">
<span class="item-text">Change</span>
</div>
<div class="next-icon">
<span class="fonticon fonticon-right-open">
::before
</span>
</div>
</li>
...
<div class="dropdown-menu dropdown-menu-root responsive scroller scroller-
root bottom-right conceal">
<div class="no-native-scrollbars scroller-content">
<ul class="dropdown-menu-wrap" tabindex="1">
<li class="item item-back" id="itm-a7b1b6" title>
<div class="back-icon">
<span class="item-text">Change</span>
</li>
<li class="item topbar-menu-dd-item" id="cbf405dc-5f99-4efd-810d-
a69666da9c75/c172" title name="cbf405dc-5f99-4efd-810d-
a69666da9c75/c172">
<div class="item item-template" id="cbf405dc-5f99-4efd-810d-
a69666da9c75/c172">
<span class="item-text">Create Change Order</span>
</div>
</li>
...
</div>
我添加的“...”表示代码中不相关的其他部分。
【问题讨论】:
-
显示你的元素代码
-
显示你的元素 HTML 代码 无法理解它的结构。为什么你必须悬停某些东西而不是单击下拉菜单然后单击菜单项?
-
@Somber 无法通过单击打开下拉菜单中的不同列表项,单击时没有任何反应,而是必须将鼠标光标悬停在所需的列表项上扩张。我在问题中添加了 HTML 结构。
标签: c# visual-studio selenium hover