FluentWait类型的直到(Function <?super WebDriver,V>)方法不适用于以下参数(布尔值,ExpectedCondition <WebElement>)

时间:2019-09-26 10:04:27

标签: java selenium selenium-webdriver webdriver webdriverwait

我给了两个条件,webdriver在继续执行之前需要等待两个条件之一满足。为此,我正在使用显式等待以及ExpectedConditions.or

我尝试使用以下代码:

new WebDriverWait(driver.getDriver(),30).until(
    ExpectedConditions.or(
        ExpectedConditions.jsReturnsValue("return document.ReadyState")).equals("complete"),    
        ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.Id("name"))
    )
);

我收到错误消息:

The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (boolean, ExpectedCondition<WebElement>)

请注意,我使用的是Guava 23.0,它已作为依赖项添加到pom.xml中。

2 个答案:

答案 0 :(得分:3)

ExpectedConditions.or接收ExpectedCondition作为参数。当您将ExpectedConditions.jsReturnsValue的结果与字符串进行比较时,会将所有表达式都更改为boolean

您可以创建自定义ExpectedCondition来变形

public static ExpectedCondition<Object> customeJsReturnsValue(final String javaScript) {
    return new ExpectedCondition<Object>() {
        @Override
        public Object apply(WebDriver driver) {
            return ExpectedConditions.jsReturnsValue("return Spotfire.Busy.idle()")).equals("true")
        }
    }
}

用途:

new WebDriverWait(driver.getDriver(),30).until(
    ExpectedConditions.or(
        customeJsReturnsValue("return document.ReadyState")).equals("complete"),
        ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.Id("name"))
    )
);

答案 1 :(得分:0)

jsReturnsValue()

jsReturnsValue() ExpectedConditions 返回 Object ,并定义为:

public static ExpectedCondition<java.lang.Object> jsReturnsValue(java.lang.String javaScript)

An expectation for String value from javascript

Parameters:
    javaScript - as executable js line

Returns:
    true once js return string

visibilityOfElementLocated()

visibilityOfElementLocated() ExpectedConditions 返回 WebElement ,并定义为:

public static ExpectedCondition<WebElement> visibilityOfElementLocated(By locator)

An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Parameters:
locator - used to find the element

Returns:
the WebElement once it is located and visible

此错误消息...

The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (boolean, ExpectedCondition<WebElement>)

...表示until()不适用于多种返回类型booleanWebElement

在您的用例中,

  • jsReturnsValue()返回一个 Object
  • visibilityOfElementLocated()返回一个 WebElement

解决方案

如果您的用例是关于某个 WebElement 的可见性的,则可以放心地忽略对'document.readyState'等于“ complete”的关注< / strong>,因为这将是一项开销。您可以在Selenium IE WebDriver only works while debugging中找到相关的讨论。

但是,要将until()的{​​{1}}用于类似类型数据类型的多个元素,可以遵循以下示例:

  • 等待visibilityOfElementLocated()By.xpath("//span[@id='id1']")中的任意一个元素就像:

    By.xpath("//span[@id='id2']")

参考

您可以在

中找到一些相关的详细讨论。

Outro

Do we have any generic function to check if page has completely loaded in Selenium