0

Is it possible to execute a command while waiting for an element to appear? In particular I'm waiting for an element to appear but it's not going to appear unless I refresh the page. Here's my code

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

The element I'm waiting for is never going to appear unless I hit the app's refresh button. How can I refresh during the 30 second timeout that I set?

7
  • Why won't you just refresh and then wait for the element? Why do you have to refresh during the wait? Commented Jan 9, 2013 at 12:48
  • Well I do a refresh before the wait but the element may not still not be there in which case it will fail because no matter how long I wait unless I refresh again the element won't appear. I guess I could put the wait inside a loop so I can refresh and wait several times. Commented Jan 9, 2013 at 15:57
  • So you have to refresh multiple times? Do you have the limit for refreshes? (number or time) Commented Jan 9, 2013 at 16:32
  • I would have to refresh until the element pops up, every 5 seconds for instance and if at the 30 second timeout the element is still not there then I give up and exit. Commented Jan 9, 2013 at 18:31
  • I've updated my answer: checks for 5 seconds then refreshes... all this is happening for 30 seconds Commented Jan 9, 2013 at 18:33

2 Answers 2

1

You can do something like this:

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

It will try to get the element for 30 seconds, refreshing the page every 5 seconds.

Sign up to request clarification or add additional context in comments.

Comments

0

I would add more lines to that code: basically, if state.isEmpty() then use a Webdriver "Action" class to move the mouse to the refresh button (you app would need a refresh button inside of it for this to work) then run the wait one more time to verify it appears.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.