首页 \ 问答 \ 做循环和预期条件 - 可以一起使用吗?(Do While Loop and ExpectedConditions - Possible to use together?)

做循环和预期条件 - 可以一起使用吗?(Do While Loop and ExpectedConditions - Possible to use together?)

做循环和预期条件 - 可以一起使用吗?

  1. 我得到奇怪的无法找到元素/超时异常与下面列出的方法。

2.是否可以添加while循环,以便该方法可以多次检查和执行?

    public void waitAndClickElement(WebElement element) throws InterruptedException {
    try {
        this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
        System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
    }catch (Exception e) {
        System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
        Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
    }
}

Do While Loop and ExpectedConditions - Possible to use together?

  1. Iam getting the odd unable to locate element / timeout exception with the method listed below.

2. Is it possible to add a while loop so the method can check and execute more than once?

    public void waitAndClickElement(WebElement element) throws InterruptedException {
    try {
        this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
        System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
    }catch (Exception e) {
        System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
        Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
    }
}

原文:https://stackoverflow.com/questions/42483776
更新时间:2023-04-24 10:04

最满意答案

  1. 使用Jon Skeet提供的lambda。 也许他也可以解释为什么ParameterExpression使用起来非常痛苦并且需要使用相同的实例,而不是能够通过名称匹配:)

  2. 修改此行:

Type[] exprArgTypes = { query.ElementType };

exprArgTypes是类型参数

IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate).

正如您所看到的,它只有一个类型参数 - TSource ,即Purchase 。 你有效地做了什么,调用Where方法有两个类型参数,如下所示:

IQueryable<Purchase> Where<Purchase, bool>(this IQueryable<Purchase> source, Expression<Func<Purchase, bool>> predicate)

一旦这两个修复程序都在表达式运行中没有问题。


  1. Use the lambda that Jon Skeet supplied. Perhaps he can also explain why ParameterExpression is so painful to use and requires using the same instance, instead of being able to be matched by name :)

  2. Modify this line:

Type[] exprArgTypes = { query.ElementType };

exprArgTypes is type parameters to

IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate).

As you can see it only has one type parameter - TSource, which is Purchase. What you were doing, effectively, was calling Where method with two type parameters like below:

IQueryable<Purchase> Where<Purchase, bool>(this IQueryable<Purchase> source, Expression<Func<Purchase, bool>> predicate)

Once both of those fixes are in the expression runs with no problem.

相关问答

更多