【问题标题】:Selenium MoveToElement not working if element not found by XPath如果 XPath 找不到元素,Selenium MoveToElement 不起作用
【发布时间】: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


    【解决方案1】:

    在 Selenium 中使用 Actions 时,您必须最终调用 Perform() 方法,否则这些操作只会在内部收集,而不会在浏览器中执行。

    你可以打电话

    action.Perform();
    

    action.Build().Perform();
    

    没关系。如果省略Build(),则Perform() 会隐式调用它。

    【讨论】:

    • 这非常有效。不敢相信这么简单,谢谢!
    【解决方案2】:

    我最近遇到了类似的问题,我的解决方案是把所有事情都作为一个动作来执行:

    action.MoveToElement(moveToItem)
          .MoveByOffset(moveToItem.Location.X, moveToItem.Location.Y)
          .MoveToElement(Page.FindElement("a[href='MembersTestPage']", Page.FindBy.CssSelector))
          .click(Page.FindElement("a[href='MembersTestPage']", Page.FindBy.CssSelector))
          .build()
          .perform();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      相关资源
      最近更新 更多