One approach is to use FluentWait and a Predicate available with
Selenium2. The advantage of this approach is that element polling
mechanism is configurable.
The code example below waits for 1 second and polls for a textarea
every 100 milliseconds.
FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate<By>() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");
Another approach is to use ExpectedCondition and WebDriverWait strategy. The code below waits for 20 seconds or till the element is available, whichever is the earliest.
public ExpectedCondition<WebElement> visibilityOfElementLocated(final By by) { return new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver driver) { WebElement element = driver.findElement(by); return element.isDisplayed() ? element : null; } }; } public void performSomeAction() { .. .. Wait<WebDriver> wait = new WebDriverWait(driver, 20); WebElement element = wait.until(visibilityOfElementLocated(By.tagName("a"))); .. }
Comments
Post a Comment