如何检查点击是否确实起作用,或替代解决方案

问题描述 投票:0回答:1

我正在使用NUnit在Selenium中创建一些测试。我有一些问题,因为某些按钮没有正确加载,因此无法点击。我确实有一个等待,应该等到按钮可单击为止,但是实际上它们似乎是可以单击的,并且单击失败。我可以看到该元素在单击之前确实具有正确的href链接,但没有任何反应。

单击之前的静态延迟会“修复”它,但是它是一个不好的解决方案,它会减慢整个测试过程,并经常在压力测试期间中断。

我很确定这是页面上的javascript运行缓慢并且在点击之前未正确初始化。

我当时在考虑,而不是检查它是否可点击,而是检查该点击是否做了任何事情。我想到了在点击前后匹配页面源,但是并不是所有的点击都一定会改变html,因此反而破坏了其他测试。

这是我当前的点击方法。但是,等待似乎毫无用处。

    public void click(IWebElement element)
    {
        IsDisplayed(element);

        Console.Write("Clicking " + element.GetAttribute("href"));

        WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
        wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(element));

        element.Click();

        Console.WriteLine(" ✓");
    }

经常失败的测试是我只是打开页面并在显示了一些元素后单击一个按钮。

c# selenium nunit webdriverwait expected-condition
1个回答
0
投票

似乎您很近。当您为ElementToBeClickable()生成WebDriverWait时,一旦返回元素,就需要在其上调用click()。实际上,您的代码块将是:

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(element)).Click();

更新

作为替代,您可以按如下方式使用IJavaScriptExecutor中的ExecuteScript()

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(element)));
© www.soinside.com 2019 - 2024. All rights reserved.