【问题标题】:How to wait until getAttribute return specific string (text) in selenium如何等到getAttribute在硒中返回特定的字符串(文本)
【发布时间】:2021-04-24 15:48:17
【问题描述】:

我有一个问题。 在我正在测试他们的页面中是一个字段,其中包括 . 我希望硒等到

getWebDriver().findElement((locator)).getAttribute("value");

会返回一个特定的文本。 他们是硒等到getAttribute返回自定义文本的一种方式吗?

这是穹顶 这是屏幕 我希望 selenium 等到 BBBOOBP 显示出来。

【问题讨论】:

    标签: selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您应该使用 Selenium Web 驱动程序显式等待条件来等待带有文本的元素出现在 DOM 中。

    带有不同语言示例的官方文档: https://www.selenium.dev/documentation/en/webdriver/waits/#explicit-wait

    如果这对你没有帮助,请告诉我。

    【讨论】:

    • 这是问题 getText() 不是我需要的它返回 null,我需要一些东西来获取属性函数。所有的等待都是为了获取我需要获取属性的文本
    • 您可以根据文本更改您的 xpath 并查看等到它的可见性。 selenium 还提供基于属性的等待:ExpectedConditions.attributeContains(locator, attribute, value) ExpectedConditions.attributeToBe(locator, attribute, value)
    【解决方案2】:

    这解决了我的问题

     public void waitForElementAttributeEqualsString(By locator, String expectedString)
        {
            WebDriver driver = getWebDriver();
            WebElement element = driver.findElement(locator);
            WebDriverWait wait = new WebDriverWait(driver, EXPLICIT_WAITE);
            ExpectedCondition<Boolean> elementAttributeEqualsString = arg0 -> element.getAttribute("value").equals
                    (expectedString);
            wait.until(elementAttributeEqualsString);
        }
    

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2021-12-31
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多