繁体   English   中英

selenium 等待刷新页面后,webelement 的状态从初始值“xxx”变为更改值“yyy”时如何处理?

[英]How to tackle when the status of an webelement changes from initial value "xxx" to changed value "yyy" after the page refresh with selenium wait?

这是我在没有运气的情况下尝试过的,
不知道我哪里出错了?

   if (("xxx").equals(messageStatus)) {      
   FluentWait<WebDriver> wait = new FluentWait<WebDriver> (driver)            
                .withTimeout(90, TimeUnit.SECONDS)                  
                .pollingEvery(500, TimeUnit.MILLISECONDS)            
                .ignoring(NoSuchElementException.class);              

  wait.until(new Function<WebDriver, WebElement>() {      
            public WebElement apply(WebDriver webDriver) {            
           String messageStatus =
  MessageLogPage.messageStatus.getText();            
                if (messageStatus.equals("yyy")){              
                    MessageLogPage.receivedPresentation.click();            
                }      
                    return null;          
                 }                
        });    

非常感谢任何帮助/建议。提前致谢!

我认为问题可能在于,如果您的页面真正刷新,对MessageLogPage.messageStatus的旧引用将无效。 即不仅 messageStatus 文本发生变化,而且元素实例本身也会发生变化。

所以我会在每次获取文本之前更改算法以获取元素。 另外你不应该点击里面等待,在之后做:

  wait.until(new ExpectedCondition<WebElement>() {      
           @Override public WebElement apply(WebDriver webDriver) {            
               try {
                   WebElement element = webDriver.findElement(By.<...>); // however you can locate MessageLogPage.messageStatus
                   if(element.getText().equals("yyy")) {
                       return element;
                   }      
                }
                catch(Exception e) { } // keep going (retun null)
                return null;                
        });    

wait.until会多次调用此函数,直到返回值不为空或超时已过期,在这种情况下它会抛出异常。 所以wait.until之后的下一行,可以是点击:

 wait.until(...); // above code
 MessageLogPage.receivedPresentation.click(); // we are here only if wait.until did not throw the exception

尽管如果页面刷新, MessageLogPage.receivedPresentation也可能无效。

您甚至可以通过返回Boolean进一步简化它,因为您在找到它后并不真正关心它:

  wait.until(new ExpectedCondition<Boolean>() {      
           @Override public Boolean apply(WebDriver webDriver) {            
               try {
                   WebElement element = webDriver.findElement(By.<...>); // however you can locate MessageLogPage.messageStatus
                   return element.getText().equals("yyy");
               }
               catch(Exception e) { } // keep going (return false)
               return false;          
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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