Selenium-如何在WebDriverWait wait.until运行时执行命令

Selenium-如何在WebDriverWait wait.until运行时执行命令,selenium,webdriver,selenium-webdriver,Selenium,Webdriver,Selenium Webdriver,是否可以在等待元素出现时执行命令?特别是,我正在等待一个元素出现,但除非刷新页面,否则它不会出现。这是我的密码 wait = new WebDriverWait(driver, 30); element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_here"))); state = element.getText(); 我等待的元素永远不会出现,除非我点击应用程序的刷新按钮。如何在设置的3

是否可以在等待元素出现时执行命令?特别是,我正在等待一个元素出现,但除非刷新页面,否则它不会出现。这是我的密码

wait = new WebDriverWait(driver, 30);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_here")));
state = element.getText();

我等待的元素永远不会出现,除非我点击应用程序的刷新按钮。如何在设置的30秒超时期间刷新

我会在代码中添加更多行:基本上,如果state.isEmpty()则使用Webdriver“Action”类将鼠标移动到“刷新”按钮(你的应用程序需要在其内部安装一个“刷新”按钮才能工作),然后再运行一次“等待”来验证它是否出现。

你可以这样做:

    System.out.println("before wait");
    WebElement el = (new WebDriverWait(driver, 30))
      .until(new ExpectedCondition<WebElement>(){
        //try the following cycle for 30 seconds
        @Override
        public WebElement apply(WebDriver driver) {
            //refresh the page
            driver.navigate().refresh();

            System.out.println("Refreshed");
            //look for element for 5 seconds
            WebElement sameElement = null;
            try{
                sameElement = (new WebDriverWait(driver, 5))
                      .until(new ExpectedCondition<WebElement>(){
                    @Override
                    public WebElement apply(WebDriver driver) {
                        System.out.println("Looking for the element");
                        return driver.findElement(By.id("theElementYouAreLookingFor"));
                    }});    
            } catch (TimeoutException e){
                // if NULL is returns the cycle starts again
                return sameElement;
            } 
            return sameElement;
    }});        
    System.out.println("Done");
System.out.println(“等待前”);
WebElement el=(新的WebDriverWait(驱动程序,30))
.直到(新的预期条件){
//尝试以下循环30秒
@凌驾
公共WebElement应用(WebDriver){
//刷新页面
driver.navigate().refresh();
System.out.println(“刷新”);
//查找元素5秒钟
WebElement SameeElement=null;
试一试{
SameeElement=(新的WebDriverWait(驱动程序,5))
.直到(新的预期条件){
@凌驾
公共WebElement应用(WebDriver){
System.out.println(“查找元素”);
返回driver.findElement(By.id(“您正在寻找的元素”);
}});    
}捕获(超时异常e){
//如果返回NULL,则循环再次开始
返回相同元素;
} 
返回相同元素;
}});        
系统输出打印项次(“完成”);

它将尝试获取元素30秒,每5秒刷新一次页面。

为什么不刷新然后等待元素?为什么在等待期间必须刷新?我在等待之前进行了刷新,但是元素可能仍然不在那里,在这种情况下,它将失败,因为无论我等待多长时间,除非我再次刷新,否则元素都不会出现。我想我可以把等待放在一个循环中,这样我可以刷新并等待几次。所以你必须刷新多次?你们有复习的限制吗?(数字或时间)我必须刷新,直到元素弹出,例如每5秒刷新一次,如果在30秒超时时元素仍然不存在,则我放弃并退出。我已更新我的答案:检查5秒,然后刷新。。。这一切持续了30秒