Selenium C#显示如何检查禁用的元素

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

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

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

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的方法的代码片段:

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="">

如何检查此元素是否存在?它是否被禁用或启用无关紧要。谢谢,里亚兹

c# selenium selenium-webdriver specflow
2个回答
0
投票

我现在找到了答案。该元素位于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();            
    }

-1
投票
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();
© www.soinside.com 2019 - 2024. All rights reserved.