有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java添加等待消息

我使用以下代码在登录后显示通知:

<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>

我使用此Selenium代码从通知中获取文本,这些通知以1-2秒的延迟逐个显示:

            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);
    }

我从第一条消息“登录成功”中获取值。但是对于下一条消息,我得到了org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@aria-label='Default warehouse retrieved.']"}

看起来我需要添加一些睡眠或侦听器消息才能显示。有什么解决办法吗


共 (1) 个答案

  1. # 1 楼答案

    不涉及iframe,可能需要引入WebDriverWait来解决此问题

    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());
    

    了解有关显式等待的详细信息official docs