Selenium c# - 等待文本不可见但似乎不起作用

Selenium c# - waiting for text not to be visible but doesn't seem to work

我是 Selenium 和 C# 的新手,我创建了一个自动化测试,通过它我创建了一些东西并立即将其删除(全部在同一个测试中)。

我测试的最后一步是验证我删除的项目的名称不再可见 - 这就是我似乎被卡住的地方。

我在模态对话框中,项目在后台可见,所以一旦我在模态对话框中确认删除,下一步是验证项目名称不再可见,但代码确实看到了name 并因此抛出异常,因为它是 true 而不是我预期的 false 结果。

查看下面我使用的代码:

public bool DeletedCategoryNoLongerVisible(string CategoryDisplayName) {
  try {
    WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='10_anchor']")));
    Driver.FindElement(By.XPath($ "//*[@class='jstree-anchor'][text()='{CategoryDisplayName}']"));
    return false;
  } catch (Exception) {
    return true;
  }
}

您可以使用 InvisibilityOfElementWithText:

等待目标元素丢失或显示的文本为空
new WebDriverWait(Driver, TimeSpan.FromSeconds(20))
  .Until(ExpectedConditions.InvisibilityOfElementWithText(By.XPath(...), CategoryDisplayName));

并且如果该元素被另一个元素隐藏,则尝试单击它直到失败:

new WebDriverWait(Driver, TimeSpan.FromSeconds(20))
  .Until((driver) => {
    try {
        driver.FindElement(By.XPath(...)).Click();
        return false;
    } catch (WebDriverException) { 
        return true;
    }
  });