C# 悬停在元素上?
使用VisualStudio和Selenium,我试图将鼠标悬停在下拉菜单中的某个元素上,以便单击其中的“隐藏”元素。这是不可能的只是点击,我必须悬停 我尝试了几种不同的方法,但没有成功: 首先,我尝试将鼠标悬停在第一个元素上,如下所示:C# 悬停在元素上?,c#,visual-studio,selenium,hover,C#,Visual Studio,Selenium,Hover,使用VisualStudio和Selenium,我试图将鼠标悬停在下拉菜单中的某个元素上,以便单击其中的“隐藏”元素。这是不可能的只是点击,我必须悬停 我尝试了几种不同的方法,但没有成功: 首先,我尝试将鼠标悬停在第一个元素上,如下所示: var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); Actions builder = new Actions(driver); IWebElement hoverElement =
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();
<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>
这个方法使第一个下拉列表元素被“标记”,但由于某种原因,下拉列表没有打开,所以我无法单击第二个元素
然后我尝试将鼠标悬停在第一个元素上,并在同一命令中单击第二个元素:
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')]"
第二个要素:
By.XPath("//span[contains(@class,'item-text')][contains(text(),'Change')]"
By.XPath("//span[contains(@class,'item-text')][contains(text(),'Create Change Order')]"
更新:
悬停下拉菜单部分的HTML结构如下所示:
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();
<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>
-
改变
::之前
...
-
改变
-
创建变更单
...
我添加的“…”表示代码中不相关的其他部分
当我将鼠标悬停在“更改”上时,会显示“创建更改单”,我可以单击它。我通常使用定制的C#Selenium点击功能
public async Task<bool> TryClick(By by, int mode = 0)
{
try
{
if (mode == 0)
{
Driver.ExecuteScript("arguments[0].click('');", ((RemoteWebDriver)Driver).FindElement(by));
}
else if (mode == 1)
{
Driver.FindElement(by).Click();
}
else if (mode == 2)
{
Actions action = new Actions(Driver);
action.MoveToElement(Driver.FindElement(by)).Perform();
action.Click(Driver.FindElement(by)).Perform();
}
return true;
}
catch (Exception ex) { }
return false;
}
我不确定这是否有帮助。
但是正如@Somber所提到的,如果您共享URL并解释要实现什么,那么获得帮助会更好。仅使用悬停(MoveToElement
)不足以激活和扩展下拉菜单。我还需要使用MoveByOffset
稍微移动鼠标
菜单展开后,单击第二个元素没有问题
//1. Hover mouse over "Change" and move it slightly to the left to open up the menu
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var DropDown1 = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//span[contains(@class,'item-text')][contains(text(),'Change')]")));
Actions action = new Actions(driver);
action.MoveToElement(DropDown1).Perform();
action.MoveByOffset(-10, -10).Perform();
//2. Click on "Create Change Order"
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[contains(@class,'item-text')][contains(text(),'Create Change Order')]"))).Click();
显示您的元素代码显示您的元素HTML代码无法理解其结构。为什么您必须将鼠标悬停在某个位置,而不是单击某个下拉菜单,然后再单击某个菜单项?@Somber单击无法打开下拉菜单中的不同列表项,单击时不会发生任何情况,您必须将鼠标光标悬停在要展开的列表项上。我在问题中添加了HTML结构。我尝试按照您的建议使用操作,但出现了一个错误:“Message:OpenQA.Selenium.WebDriverException:javascript错误:无法读取未定义”的“left”属性,不确定这意味着什么。我还试着按照您的建议使用“Driver.ExecuteScript”,但我的驱动程序不包含“ExcecuteScript”。如果你想看一看,我已经用部分HTML代码更新了这个问题。你能分享这个链接吗?我想先在我这边测试一下。不幸的是不可能。该页面只能通过我工作的公司访问。