Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do WebDriverWait correctly with two conditions

Tags:

java

selenium

I created the code:

new WebDriverWait(driver,100).until(
    new ExpectedCondition<Boolean>(){

        @Override
        public Boolean apply(WebDriver driver){

            if(driver.findElement(byLogin).isDisplayed()){

                System.out.println("test1");
                return true;
            }
            else if(driver.findElement(byConc).isEnabled()){
                System.out.println("test1");
                driver.findElement(byShop).click();
                return true;
            }
            return false;
        }
    }
);

Code after "else if" never executes. How could I make the correct ExpectedCondition having both conditions above?

like image 366
lamcpp Avatar asked Nov 07 '25 11:11

lamcpp


1 Answers

You cannot have return in both if.else if blocks if need to satisfy both conditions. Instead have another boolean variable and return that can be overwritten from both code blocks.

new WebDriverWait(driver,100).until(
    new ExpectedCondition<Boolean>(){
        boolean ind = false;

            @Override
            public Boolean apply(WebDriver driver){

                if(driver.findElement(byLogin).isDisplayed()){

                    System.out.println("test1");
                    ind = true;
                }
                else if(driver.findElement(byConc).isEnabled()){
                    System.out.println("test1");
                    driver.findElement(byShop).click();
                    ind = true;
                }
                else{
                    ind = false;
                }
                return ind;
            }
        }
);
like image 57
Saifur Avatar answered Nov 09 '25 05:11

Saifur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!