0

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

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

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

4

2 回答 2

1

你可以这样做:

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

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

于 2013-01-09T17:54:44.623 回答
0

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

于 2013-01-09T01:16:11.500 回答