javascript - Selenium - 等待返回的 javascript 脚本值匹配值

标签 javascript java selenium-webdriver

我需要等待 javascript 的结果与字符串或 boolean 值匹配。

使用这个 JavaScript:

document.getElementById('video_html5').seeking;

我得到一个“false”/“true”值,并且我需要等到该值为“false”,所以我确定视频没有在寻找,但我只找到了等待的方法javascript 命令值,而不是检查该值是否与某些文本匹配的方法。

new WebDriverWait(driver, 30)
    .until(ExpectedConditions.jsReturnsValue("return document.getElementById('video_html5').seeking;"));

因为在某些情况下我会得到一个 boolean 值以外的字符串,并且需要比较这些字符串。

我已经找到了如何在 Ruby 中做到这一点,但在 Java 中却没有:

wait_element = @wait.until { @driver.execute_script("return document.getElementById('vid1_html5_api_Shaka_api').seeking;").eql? false }

最佳答案

您可以编写自己的自定义预期条件。

public class MyCustomConditions {

  public static ExpectedCondition<Boolean> myCustomCondition() {
    return new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
        return (Boolean) ((JavascriptExecutor) driver)
          .executeScript("return document.getElementById('video_html5').seeking === 'string_value' || ... ");
      }
    };
  }
}

然后在您的测试中,您可以按如下方式使用条件。

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(MyCustomConditions.myCustomCondition());

关于javascript - Selenium - 等待返回的 javascript 脚本值匹配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41554625/

相关文章:

java - 为什么不用Spring框架?

java - 错误: not found: java Usage: selenium-standalone action [options]

python - Selenium Python 列中的表行未存储在我的列表变量中

c# - 在 C# 中将 Httpheader 添加到 selenium chrome webdriver

javascript - 改变jquery对话框的颜色

javascript - 对 CommonJS 配置文件使用全局变量

java - 在JAVA中查找直到特定日期的重复工作日

javascript - 已弃用 : DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself

javascript - 如何使 FF 将字符插入到由 onkeypress 处理程序添加和聚焦的输入中?

java - 如何获取 RFT 版本号?