selenium - 执行 Selenium 显式等待时 Katalon 中的 GroovyCastException

标签 selenium selenium-webdriver groovy katalon-studio webdriverwait

我正在尝试在 Katalon(使用 Groovy)中执行显式等待。我有以下代码:

// wait on page change to "Dashboard"
    WebDriverWait dashboardChangeWait = new WebDriverWait(driver, 3)
    /* This is causing the following Exception : 
     *   - GroovyCastException : Attempt to cast 'true' with class 'java.lang.Boolean' to class
     *      'org.openqa.selenium.WebElement'
     * */
    WebElement element = dashboardChangeWait.until(
        ExpectedConditions.textToBe(By.cssSelector('.breadcrumb-item.active'), "DASHBOARD"))

这给了我一个 GroovyCastException 。我知道WebDriverWait.until需要 Function (是的,类似 JavaScript 的编码!)参数,以及 ExpectedConditions.textToBe returns a ExpectedCondition<Boolean> until的签名是V org.openqa.selenium.support.ui.FluentWait.until(Function<Object, Object<V>> arg0) 。有没有办法在 Katalon 中执行这种类型的等待,从而避免此问题?

最佳答案

你们已经很接近了。 ExpectedConditions 方法 textToBe()定义如下:

public static ExpectedCondition<java.lang.Boolean> textToBe(By locator, java.lang.String value)

An expectation for checking WebElement with given locator has specific text

Parameters:
locator - used to find the element
value - used as expected text

Returns:
Boolean true when element has text value equal to @value

因此,您只需将返回类型更改为 boolean 而不是 WebElement,如下所示:

Boolean status = dashboardChangeWait.until(ExpectedConditions.textToBe(By.cssSelector('.breadcrumb-item.active'), "DASHBOARD"))

关于selenium - 执行 Selenium 显式等待时 Katalon 中的 GroovyCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50727673/

相关文章:

python-3.x - 如何使用 python 的 selenium 获取 href 属性?

groovy - Groovy 中的大括号语法是什么意思?

python - 机器人框架 Selenium2Library 中的无缓存刷新

python - Selenium Firefox 驱动程序从主机文件读取

selenium - 仅允许本地连接 Chrome 和 Selenium webdriver

java - 经过多次自动化努力后无法找到 android native 应用程序的验证

java - 将文件转换为 MultiPartFile

java - Android 中的 Groovy 扩展模块?

java - Xpath 不适用于 selenium Web 驱动程序

java - 错误: cannot access org. openqa.selenium.HasInputDevices,未找到类文件