简体   繁体   中英

Can I use Thread.Sleep when the loading spinner and the error message has the same element xpath?

This is the logic I am trying to implement. I press a tab in a page and if there is an error message, I System.out.println a custom error message to the console. If the error message does not occur, I proceed with other functionalities.

The problem is after I click the tab, there is a spinner that loads before loading the data. Xpath for the spinner and error message is the same. So, When the WebDriver sees the spinner, it identifies it as an error and gives me an error message.

When I add a Thread.Sleep in between, then it identifies the loaded section and is not giving me the error message.

Apart from using Thread.sleep, can I do anything to wait until the data loads properly after the spinner?

    private Boolean BenInErr() throws InterruptedException
{
    try {
        Thread.sleep(4000);
        WebDriverWait BErr = new WebDriverWait (driver, 10);
        WebElement BErrText = driver.findElement(By.xpath("//div[@id='plan-details-benefits-and-cost-content']/div/services-list/div/mc-loading-error/div/div/span"));
        //String Text = BErrText.getText();
        BErr.until(ExpectedConditions.visibilityOf(BErrText));
        return BErrText.isDisplayed();
    }

    catch (Exception Ex1)
    {
        System.out.println("Benefit information is available");
        return false;
    }
}

public void mngBenInfErr() throws InterruptedException
{
 if (BenInErr())
{
    System.out.println("There is an error in Benefits & Coverage tab in Benefit information section");
}
}

I would suggest to write a wait method for this, later you can reuse it, for example:

    public static void waitForLoaderToDisappear() {
    _wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id(YOURLOADERID)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM