【发布时间】:2016-04-06 11:51:42
【问题描述】:
Selenium Webdriver for C# 中的 MoveToElement 函数存在问题。 MoveToElement 似乎没有做任何事情。
我有以下 HTML:
<div id="rounded-navigation-with-icons">
<ul>
<li class="navigation-item">
<a href="Members" target="_self" class="navigation-item-title"></a>
<ul>
<li>
<a href="MembersTestPage" target="_self"></a>
</li>
</ul>
</li>
</ul>
</div>
最里面的列表最初是隐藏的,直到导航项被鼠标悬停在上面。
然后我使用以下代码单击 Selenium 可见的导航项标题,然后单击 MembersTestPage 链接。
public bool SearchForElement(string elementToFind, Page.FindBy by)
{
var navigation = Page.FindElement("rounded-navigation-with-icons", Page.FindBy.ID);
if (navigation != null)
{
foreach (var item in navigation.FindElements(By.ClassName("navigation-item")))
{
var titleElements = Page.FindElements("navigation-item-title", Page.FindBy.ClassName);
Actions action = new Actions(Driver.Instance);
foreach (var moveToItem in titleElements)
{
try
{
// Move to the main navigation link container element, but it doesn't work
action.MoveToElement(moveToItem);
// Move the mouse position manually to the link's location
action.MoveByOffset(moveToItem.Location.X, moveToItem.Location.Y);
// This does correctly find the element
var element = Page.FindElement("a[href='MembersTestPage']", Page.FindBy.CssSelector);
action.MoveToElement(element);
// Click returns that the element is hidden/invisible and therefore cannot be clicked
element.Click();
return true;
}
catch (Exception)
{
}
}
}
}
return false;
}
您可以看到我通过传递元素使用了 MoveToElement,也通过手动传递项目的 X 和 Y 值,但两者都不起作用。
如果我通过 XPath 找到元素,这将按预期工作。
我做错了什么?谢谢
【问题讨论】:
标签: c# selenium selenium-webdriver