roey_r
roey_r

Reputation: 21

Selenium - Reduce findelement time

When I run the webdriver it runs too fast.

The webdriver is moving the next element before the first one appears.

Can I make webdriver slower?

Thanks!

Upvotes: 1

Views: 1904

Answers (1)

bbarke
bbarke

Reputation: 309

If you are using java, you can do implicit waits on every page found here

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

but a better way of doing this is by using fluent waits, to wait for a element to appear on the page, like this

public WebElement fluentWait(final By locator){
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
                    return driver.findElement(locator);
            }
            }
      );
                       return  foo;              
   };

and you can use it by

WebElement element = fluentWait(By.id("name"));
element.click();

This is a great answer to read over answer to waits with selenium

Upvotes: 2

Related Questions