wait.until(ExpectedConditions.visibilityOf Element1 OR Element2)

后端 未结 8 1120
遇见更好的自我
遇见更好的自我 2020-12-14 12:30

I want use wait.until(ExpectedConditions) with TWO elements. I am running a test, and I need WebDriver to wait until either of Element1 OR

相关标签:
8条回答
  • 2020-12-14 12:51

    Unfortunately, there is no such a command. You can overcome this by try and catch, or I would better recommend you to use open source Ruby library Watir.

    0 讨论(0)
  • You can also use a CSSSelector like this:

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h2.someClass, h3.otherClass")));
    

    Replace someClass and otherClass with what you had in [...] in the xpath.

    0 讨论(0)
  • 2020-12-14 12:51

    If you have a variable number of conditions to satisfy, you can leverage a generic list such as Java's ArrayList<> as shown here, and then do the following:

    //My example is waiting for any one of a list of URLs (code is java)
    
        public void waitUntilUrls(List<String> urls) {
            if(null != urls && urls.size() > 0) {
                System.out.println("Waiting at " + _driver.getCurrentUrl() + " for " + urls.size() + " urls");
                List<ExpectedCondition<Boolean>> conditions = new ArrayList<>();
                for (String url : urls) {
                    conditions.add(ExpectedConditions.urlContains(url));
                }
    
                WebDriverWait wait = new WebDriverWait(_driver, 30);
                ExpectedCondition<Boolean>[] x = new ExpectedCondition[conditions.size()];
                x = conditions.toArray(x);
                wait.until(ExpectedConditions.or(x)); // "OR" selects from among your ExpectedCondition<Boolean> array
            }
        }
    
    0 讨论(0)
  • 2020-12-14 12:53

    Now there's a native solution for that, the or method, check the doc.

    You use it like so:

    driverWait.until(ExpectedConditions.or(
        ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")),
        ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything"))));
    
    0 讨论(0)
  • 2020-12-14 12:53

    There is an alternative way to wait but it isnt using expected conditions and it uses a lambda expression instead..

    wait.Until(x => driver.FindElements(By.Xpath("//h3[@class='... ']")).Count > 0 || driver.FindElements(By.Xpath("//h2[@class='... ']")).Count > 0); 
    
    0 讨论(0)
  • 2020-12-14 13:04

    There is a simple solution for this, using an Explicit Wait:

    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='FirstElement' or @id='SecondElement']")));
    

    Before this I was trying to use wait.until(ExpectedConditions.or(..., which was not compatible with selenium versions before 2.53.0.

    0 讨论(0)
提交回复
热议问题