Java 当webelement的状态从初始值更改时,如何处理;xxx“;“更改值”;yyy";使用selenium刷新页面后,是否等待?

Java 当webelement的状态从初始值更改时,如何处理;xxx“;“更改值”;yyy";使用selenium刷新页面后,是否等待?,java,selenium,selenium-webdriver,wait,Java,Selenium,Selenium Webdriver,Wait,这是我尝试过的,但运气不佳, 不知道我哪里出错了 if (("xxx").equals(messageStatus)) { FluentWait<WebDriver> wait = new FluentWait<WebDriver> (driver) .withTimeout(90, TimeUnit.SECONDS) .po

这是我尝试过的,但运气不佳,
不知道我哪里出错了

   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;          
                 }                
        });    
if((“xxx”).equals(messageStatus)){
FluentWait=新FluentWait(驱动程序)
.带超时(90,时间单位。秒)
.pollingEvery(500,时间单位为毫秒)
.忽略(NoSuchElementException.class);
wait.until(新函数(){
公共WebElement应用(WebDriver WebDriver){
字符串消息状态=
MessageLogPage.messageStatus.getText();
如果(messageStatus.equals(“yyy”){
MessageLogPage.receivedPresentation.click();
}      
返回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;                
        });    
尽管可能是
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;          
        });
等待.until(新的ExpectedCondition(){
@重写公共布尔应用(WebDriver WebDriver){
试一试{
WebElement=webDriver.findElement(By.);//但是您可以找到MessageLogPage.messageStatus
返回元素.getText().equals(“yyy”);
}
catch(异常e){}//继续(返回false)
返回false;
});
  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;          
        });