0

我有一个 Selenium 测试,它检查是否可以在测试页上看到某个字符串。如果页面没有立即加载,我的想法是使用显式等待。

WebElement element = driver.findElement(By.xpath("//*[text()='text']"));

之后我做了:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOf(element));

因此,在继续之前,它的等待时间最长为 10 秒。如果它在 10 秒内看到该元素,则测试继续。

但我在 wait.until 收到以下错误

until
(java.util.function.Function<? super org.openqa.selenium.WebDriver,java.lang.Object>)
in FluentWait cannot be applied
to
(org.openqa.selenium.support.ui.ExpectedCondition<org.openqa.selenium.WebElement>)
 

使用谷歌解决这个问题并没有帮助我。任何人都知道如何解决这个错误?

该元素包含我想要的字符串

4

2 回答 2

3

正如您在问题中提到的那样,检查是否可以在测试页上看到某个字符串作为验证点过于宽泛。理想情况下,您应该验证预期的文本是否存在于WebElement中。为此,您必须先使用任一定位器策略来识别元素,然后使用适当的ExpectedConditions诱导WebDriverWait来验证文本,如下所示:

脚步

  • 找到 WebElement

    WebElement element = driver.findElement(Locator Strategy);
    
  • 为textToBePresentInElement引入WebDriverWait

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.textToBePresentInElement(element, "text_to_be_validated"));
    
于 2018-02-16T14:45:41.267 回答
0

在我之前的情况下,我在 pom.xml 中使用了以下一个

     <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>3.141.59</version>
     </dependency>

我删除了上面的并用这个替换

      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.0.1</version>
          <scope>compile</scope>
      </dependency>

它起作用了,并且“FluentWait 中的直到(java.util.function.Function)不能应用于”这个错误从我的 intellij 类中消失了。

在较新的 selenium jar 版本中,某些东西已被弃用或无法正常工作。所以用旧的。

您还可以在 pom.xml 中专门添加以下依赖项并检查。

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.0.1</version>
    </dependency>
于 2020-12-15T12:38:57.077 回答