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:
WebDriverWait failedLoginWebDriverWait = new WebDriverWait(driver, 7);
WebElement failedLoginWebElement = failedLoginWebDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='toast-container']")));
WebElement element = failedLoginWebElement.findElement(By.xpath("//*[@id='toast-container']/div[1]/div"));
waitUntilElementNotDisplayed(element, driver);
WebElement element2 = failedLoginWebElement.findElement(By.xpath("//*[@id='toast-container']/div[2]/div"));
waitUntilElementNotDisplayed(element2, driver);
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 this output:
Login successful.
Login successful.
Login successful.
Authorized warehousees retrieved.
Visually I get all above messages. Do you know why I get 3 times the first message? I also get if I try
WebElement element2 = failedLoginWebElement.findElement(By.xpath("//*[@id='toast-container']/div[3]/div")); - element not found.
Do you know how I can get all messages text?
I have seen weird behavior with Angular and visibilityOfElementLocated
. I would suggest using a different xpath. @aria-label
in stead of @id
.
For example:
WebElement element2 = failedLoginWebElement.findElement(By.xpath("//div[@aria-label='Login successful.']"));
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.