Add wait for messages

Issue

I have this Angular code used to display notifications after login:

<div id="toast-container" class="toast-top-right toast-container">
   <div toast-component="" class="toast-success ngx-toastr ng-trigger ng-trigger-flyInOut ng-animating" style="">
      <!----><button aria-label="Close" class="toast-close-button ng-tns-c15-20 ng-star-inserted" style=""><span class="ng-tns-c15-20" aria-hidden="true">×</span></button><!----><!----><!---->
      <div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="Default warehouse retrieved." style=""> Default warehouse retrieved. </div>
      <!---->
   </div>
   <div toast-component="" class="toast-success ngx-toastr ng-trigger ng-trigger-flyInOut" style="opacity: 1;">
      <!----><button aria-label="Close" class="toast-close-button ng-tns-c15-19 ng-star-inserted" style=""><span class="ng-tns-c15-19" aria-hidden="true">×</span></button><!----><!----><!---->
      <div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="User is authorized to run the FootPrint Mobile Web application." style=""> User is authorized to run the FootPrint Mobile Web application. </div>
      <!---->
   </div>
   <div toast-component="" class="toast-success ngx-toastr ng-trigger ng-trigger-flyInOut" style="opacity: 1;">
      <!----><button aria-label="Close" class="toast-close-button ng-tns-c15-18 ng-star-inserted" style=""><span class="ng-tns-c15-18" aria-hidden="true">×</span></button><!----><!----><!---->
      <div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="User session info saved successfully." style=""> User session info saved successfully. </div>
      <!---->
   </div>
   <div toast-component="" class="toast-success ngx-toastr ng-trigger ng-trigger-flyInOut" style="opacity: 1;">
      <!----><button aria-label="Close" class="toast-close-button ng-tns-c15-17 ng-star-inserted" style=""><span class="ng-tns-c15-17" aria-hidden="true">×</span></button><!----><!----><!---->
      <div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="Login successful." style=""> Login successful. </div>
      <!---->
   </div>
</div>

I use this Selenium code to get the text from notifications which are displayed one by one with 1-2 seconds delay:

            WebDriverWait failedLoginWebDriverWait = new WebDriverWait(driver, 7);
    
            WebElement failedLoginWebElement = failedLoginWebDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='toast-container']")));
    
    
    WebElement element2d = failedLoginWebElement.findElement(By.xpath("//div[@aria-label='Login successful.']"));

    System.out.println("!!!!!!!!!!! " + element2d.getText());

    WebElement element3d = failedLoginWebElement.findElement(By.xpath("//div[@aria-label='Default warehouse retrieved.']"));

    System.out.println("!!!!!!!!!!! " + element3d.getText());

    WebElement element4d = failedLoginWebElement.findElement(By.xpath("//div[@aria-label='Authorized warehousees retrieved.']"));

    System.out.println("!!!!!!!!!!! " + element4d.getText());

    WebElement element5d = failedLoginWebElement.findElement(By.xpath("//div[@aria-label='User is authorized to run the FootPrint Mobile Web application.']"));

    System.out.println("!!!!!!!!!!! " + element5d.getText());


protected void waitUntilElementNotDisplayed(final WebElement webElement, WebDriver driver) {
        WebDriverWait wait = new WebDriverWait(driver, 5);
        ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver arg0) {
                try {
                    webElement.isDisplayed();
                    System.out.println(webElement.getText());
                    return false;
                }
                catch (NoSuchElementException e ) {
                    return true;
                }
                catch (StaleElementReferenceException f) {
                    return true;
                }
            }
        };
        wait.until(elementIsDisplayed);
    }

I get the value from the first message ‘Login successful.’. But for the next message I get org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@aria-label='Default warehouse retrieved.']"}.

Looks like I need to add some sleep or listener messages to appear. Is there some solution?

Solution

There is no iframe involved, Probably you would need to introduced WebDriverWait to resolve this issue.

WebDriverWait wait = new WebDriverWait(driver, 20); 
WebElement element3d = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@aria-label='Default warehouse retrieved.']")));
System.out.println("!!!!!!!!!!! " + element3d.getText());

Learn more about Explicit wait here official docs

Answered By – cruisepandey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published