wait.until(ExpectedConditions.visibilityOf Element1 OR Element2)

后端 未结 8 1139
遇见更好的自我
遇见更好的自我 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 13:14

    This is the method I declared in my Helper class, it works like a charm. Just create your own ExpectedCondition and make it return any of elements found by locators:

    public static ExpectedCondition oneOfElementsLocatedVisible(By... args)
    {
        final List byes = Arrays.asList(args);
        return new ExpectedCondition()
        {
            @Override
            public Boolean apply(WebDriver driver)
            {
                for (By by : byes)
                {
                    WebElement el;
                    try {el = driver.findElement(by);} catch (Exception r) {continue;}
                    if (el.isDisplayed()) return el;
                }
                return false;
            }
        };
    }
    

    And then you can use it this way:

    Wait wait = new WebDriverWait(driver, Timeouts.WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
    WebElement webElement = (WebElement) wait.until(
            Helper.oneOfElementsLocatedVisible(
                    By.xpath(SERVICE_TITLE_LOCATOR), 
                    By.xpath(ATTENTION_REQUEST_ALREADY_PRESENTS_WINDOW_LOCATOR)
            )
    );
    

    Here SERVICE_TITLE_LOCATOR and ATTENTION_REQUEST_ALREADY_PRESENTS_WINDOW_LOCATOR are two static locators for page.

提交回复
热议问题