Selenium-等待,直到返回的javascript脚本值匹配值 [英] Selenium - Wait until returned javascript script value match value

查看:174
本文介绍了Selenium-等待,直到返回的javascript脚本值匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要等到javascript的结果匹配字符串或布尔值.

I need to wait until the result of a javascript match a string or a boolean value.

使用此javascript:

With this javascript:

document.getElementById('video_html5').seeking;

我得到一个假"/真"值,我需要等到该值是假",所以我确定视频不是在寻找视频,但我只是找到了一种等待视频的方式. javascript命令值,而不是检查值是否与某些文本匹配的方法.

I get a "false" / "true" value, and i need to wait until the value is "false", so I'm sure that the video is not seeking, but I only found the way to wait for a javascript command value and not the way to check that the value matches some text.

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

因为在某些情况下,我得到的字符串不是布尔值,所以需要比较那些字符串.

Because in some cases I get a string other than boolean and need to compare those strings.

我已经找到了在Ruby中而不是在Java中的方法:

I have found how to do it in Ruby but not in Java:

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

推荐答案

您可以编写自己的自定义期望条件.

You can write your own custom expected conditions.

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' || ... ");
      }
    };
  }
}

然后在测试中,您可以使用以下条件.

And then in your tests you can use the condition as follows.

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

这篇关于Selenium-等待,直到返回的javascript脚本值匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆