我如何使用“ GetAttribute”或“ .Text”获取StaleElementReferenceException的值来验证硒中的Assert.IsTrue

时间:2019-07-02 18:06:58

标签: .net selenium specflow

我尝试通过代码获取StaleElementReferenceException的值:

var valuePerfil1 = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[contains(.,'Analistas') and contains(@class, 'x-grid3-cell-inner x-grid3-col-id x-unselectable')]")));

wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));

try
{
    valuePerfil1.Click();
    selectValue.Click();
}

catch (StaleElementReferenceException)
{
  String recebe = valuePerfil1.Text;
  _valuePerfil1 = recebe;
}

但是在Assert.IsTrue(_valuePerfil1.Contains("Analista"));中进行比较时会出现此错误:_valuePerfil1 era null

我如何使用valuePerfil1.GetAttribute("Text");valuePerfil1.Text;来获取此值

1 个答案:

答案 0 :(得分:0)

StaleElementException表示IWebElement不再在浏览器中引用有效的HTML标记或“文档对象模型”节点。 _valuePerfil1字段为空,因为valuePerfil1变量未绑定到浏览器中加载的HTML标记,因此其变量Text的属性很可能是null。 / p>

_valuePerfil1.Text元素为“陈旧”时,我希望null_valuePerfil1或抛出异常。