Question

After creating an Activation Code it takes 1-60 seconds for the code to be uploaded into the system. So right after creating a new code, I want to use WebDriverWait for 60 sec to make sure and in this time period every 3 sec I want to click on Search Button. Is there any way to do this?

(new WebDriverWait(driver, 60))
.until(ExpectedConditions.textToBePresentInElement(By.xpath("//*[@id='searchResults']"), activationCode));
Was it helpful?

Solution 2

This comes "for free" with WebDriverWait.

There is a value you can set when creating the WebDriverWait to tell it how often it should be attempting to run your code (which is clicking the search button):

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html#pollingEvery(long, java.util.concurrent.TimeUnit)

So just set pollingEvery equal to three seconds.

OTHER TIPS

You can use for loop for this purpose. Wait for 3 seconds in a loop and come out of loop if your condition (code generation) is fulfilled.

By doing this you need not to wait for 60 seconds if code is generated in say 10 seconds. You will come out after 12 seconds wait.

Loop approach worked fine for me, thanks for the advice h4k3r.

while(dynamicSearch(activationCode,"//*[@id='searchResults']") && key<20)
{
  driver.findElement(By.xpath(".//*[@id='searchItem']")).click();
  key++;
}

This is the method part.

public static boolean dynamicSearch(String activationCode, String xpathAdress)

{ try

{ (new WebDriverWait(driver, 3))

.until(ExpectedConditions.textToBePresentInElement(By.xpath(xpathAdress), activationCode));

return false; }

catch (Exception e)
{ return true; }

One more suggestion :)

Use CSS Selector over XPATH as CSS Selector is faster than XPATH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top