【问题标题】:Selenium: How to wait for a string inside InnerHtml (SeleniumExtras.WaitHelpers)Selenium:如何等待 InnerHtml 中的字符串 (SeleniumExtras.WaitHelpers)
【发布时间】:2022-01-23 15:20:22
【问题描述】:

我想在我的 selenium 测试中创建一个方法,该方法确实会等到 InnerHTML 中具有特定字符串的元素显示出来。

目前我这里有这个:

string textToCompare = "Hello";
w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath(PathElement))).GetAttribute("innerHTML");

在上面的代码行中,我只是在等待PathElement 并获取innerHTML 的属性。

但我希望它的行为类似于:等待其 innerHTML 中包含 textToCompare 字符串的元素

如果硒还有其他等待方法,我会接受任何建议。

或者有没有办法使用 PathElement 变量直接导航到 InnerHTML?

这是网页的检查:

【问题讨论】:

  • 试试流利的等待
  • PathElement是如何定义的?
  • PathElement 只是一个简单的 XPath。在这种情况下string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4']"。我们的网站使用的是 mudblazor,它可以动态创建元素。所以这就是为什么元素的命名很奇怪并且没有特定的 ID。
  • 我添加了inspect 的截图。红色圆圈是我想使用等待方法等待的文本。我知道我可以轻松地等待PathElement,但我想等待innerHTML 中的特定字符串

标签: .net selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

要等到具有特定字符串的元素显示为 InnerHTML 而不是 ElementExists(),您需要为 ElementIsVisible() 引入 WebDriverWait,您可以使用关注Locator Strategies

  • 使用ElementIsVisible()Text属性:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4']"
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • 使用ElementIsVisible() 并以特定文本开头:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and starts-with(., 'Importiere')]"   
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • 使用ElementIsVisible() 并包含特定文本:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and contains(., 'Importiere')]"  
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • 使用ElementIsVisible() 和通用文本:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and text()]" 
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 1970-01-01
    • 2014-05-11
    • 2021-05-07
    • 1970-01-01
    • 2023-03-10
    • 2010-09-24
    • 2011-08-22
    • 2020-10-04
    相关资源
    最近更新 更多