【问题标题】:Selenium C# how to check a disabled element is displayedSelenium C#如何检查禁用的元素是否显示
【发布时间】:2019-04-05 10:50:53
【问题描述】:

我正在检查一个元素是否被显示。我的断言返回 false,我发现这是因为该元素被禁用。
我想检查元素是否显示,无论它是启用还是禁用。

我的代码 sn-p 是(从我们的框架中检查元素是否显示的方法):

public bool IsElementPresent(IWebDriver browser, IWebElement element)
        {
            return utility.Element.IsDisplayed(element).Invoke(browser);
        }


public Func<IWebDriver, bool> IsDisplayed(IWebElement element)
        {
            return driver =>
            {
                try
                {
                    return element.Displayed;
                }
                catch (Exception e)
                {
                    MessageHandler.OutputError(e);
                    return false;
                }
            };
        }

调用IsElementPresent的方法的代码sn-p:

public void CheckSportsLoginDialogIsDisplayed()
        {
            Actions.Verify.IsElementPresent(Browser, SportsLogin).Should().BeTrue();
       }

定位器:

[FindsBy(How = How.CssSelector, Using = "input.loginButton.submitButton.loginSubmit")]
private IWebElement SportsLogin { get; set; }

HTML 元素:

<input class="loginButton submitButton loginSubmit disabled" value="Log in" type="submit" disabled="">

如何检查此元素是否存在?它是禁用还是启用都没有关系。 谢谢, 里亚兹

【问题讨论】:

  • 我刚刚对&lt;input type="text" id="name" readonly&gt; 进行了快速测试,以便它是只读的,然后使用Driver.FindElement(By.Id("name")).Displayed 并返回true。我认为这里正在发生其他事情。我知道当 Selenium 检查某些东西是否可点击时,它会检查显示和启用,这意味着 disabled 的东西仍应返回 true 以显示。您确定您的定位器指向正确的INPUT?您确定该元素在测试时实际上是可见的吗?您是否尝试过先添加等待?

标签: c# selenium selenium-webdriver specflow


【解决方案1】:

我现在找到了答案。该元素位于 iFrame 中。 IFrame 在 HTML 树中更靠前。我不得不切换到它,然后检查元素是否显示。我的代码现在可以工作了。

<iframe name="freebet" id="freebet" src="https://sports.companya.com/freebet" scrolling="auto" style="display: inline; height: 465px;" class="freebet" cd_frame_id_="9c810a320feffc91ad5fde8519f4d0cb"></iframe>

public void CheckSportsLoginDialogIsDisplayed()
    {
        Actions.Navigate.ToFrame(Browser,FrameOption.Default);                        
        Actions.Verify.IsElementPresent(Browser, SportsLogin).Should().BeTrue();            
    }

【讨论】:

  • 这就是catch (Exception e) 的问题...您捕获的超出预期并隐藏了意外错误。
  • 如上面 JeffC 所述,理想情况下,您可以捕获需要捕获的内容,尤其是在调试时。在这种情况下,由于您正在检查可见性,请捕获:ElementNotVisibleException,如 SeleniumHQ 上所示 here
【解决方案2】:
if(SportsLogin != null) // should be null if not present 
{
    bool isDisplayed = SportsLogin.Displayed;
}

或者创建一个扩展

/// <summary>
/// Requires finding element by FindElementSafe(By).
/// Returns T/F depending on if element is defined or null.
/// </summary>
/// <param name="element">Current element</param>
/// <returns>Returns T/F depending on if element is defined or null.</returns>
public static bool Exists(this IWebElement element)
{
    if (element == null)
    { return false; }
    return true;
}

那么你就可以像这样使用它了

bool exists = SportsLogin.Exist();

【讨论】:

  • 如果元素不存在,它们就不是null。在它们成为null.之前,它们会在没有被发现时抛出异常
  • @JeffC Ahh 指出,他们抛出 NoSuchElementFoundException 谢谢
  • 如果我尝试扩展建议之前提到的第一种方法,我会收到错误 Exception throw: 'OpenQA.Selenium.NoSuchElementException' in WebDriver.Support.dll
  • 登录元素显示在屏幕上。它被禁用。我关心的是检查屏幕上显示的元素是禁用还是启用。谢谢
  • @RiazLadhani 做this 回答帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2022-01-10
  • 2012-06-11
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多