C# 使用页面对象模式验证元素的状态

C# 使用页面对象模式验证元素的状态,c#,selenium,xpath,C#,Selenium,Xpath,假设我想检查我的密码是否已成功更改。使用哪种方法更好 [FindsBy(How = How.XPath, Using = "//span[@id='confirmation' and text()='Success!']")] public IWebElement PasswordChangedSuccessfullyConfirmationElement { get; protected set; } public bool IsPasswordChanged() { return

假设我想检查我的密码是否已成功更改。使用哪种方法更好

[FindsBy(How = How.XPath, Using = "//span[@id='confirmation' and text()='Success!']")]
public IWebElement PasswordChangedSuccessfullyConfirmationElement { get; protected set; }

public bool IsPasswordChanged()
{
    return  PasswordChangedSuccessfullyConfirmationElement != null && 
            PasswordChangedSuccessfullyConfirmationElement.Displayed;
}


我更喜欢第一种方法,因为它等待元素文本更改(直到隐式超时)。第二种方法不会等待元素文本更改成功。如果元素不是新的,它有时可能会失败。在第二种方法中,您必须添加等待语句以更改文本。

为什么要检查元素是否为null?
[FindsBy(How = How.Id, Using = "confirmation")]
public IWebElement PasswordChangedSuccessfullyConfirmationElement { get; protected set; }

public bool IsPasswordChanged()
{
    return  PasswordChangedSuccessfullyConfirmationElement != null && 
            PasswordChangedSuccessfullyConfirmationElement.Text == "Success!" &&
            PasswordChangedSuccessfullyConfirmationElement.Displayed;
}