Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium refresh

I am working on project where everything is saved in events so it took some time for server to respond for new data. I am using Fluent wait for pages using ajax, but this one doesn't use any ajax. So I want to refresh page check if new item is there if not refresh again. How this is achieved in Selenium 2?

I did this :

        def accountsAfterCreating = 0
        while (accountsAfterCreating <= existingAccounts) {
            driver.navigate().refresh()
            WebElement table = wait.until(new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                    return driver.findElement(By.className("table"))
                }
            })
            accountsAfterCreating = table.findElements(By.className("amount")).size()
        }

Is it correct way?

like image 657
IowA Avatar asked Dec 06 '25 00:12

IowA


2 Answers

Use Explicit wait like this In try catch block

try{

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

}

catch()

{

driver.navigate().refresh()

}

like image 59
Archana Singh Avatar answered Dec 08 '25 21:12

Archana Singh


I usually use this method to wait for any html tag. We can also specify the wait time.

public boolean waitForElement(WebElement ele, String xpath, int seconds) throws InterruptedException{
    //returns true if the element appears within the time
    //false when timed out
    int t=0;
    while(t<seconds*10){
        if(ele.findElements(By.xpath(xpath)).size()>0)
            return true;
        else{
            Thread.sleep(100);
            t++;
            continue;
        }
    }       
    System.out.println("waited for "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
    return false;
}
like image 28
cegprakash Avatar answered Dec 08 '25 22:12

cegprakash