Search code examples
gwtseleniumselenium-webdrivergwt2selenium-server

How can I overcome Element id exception in Selenium?


T set 'id' for GWT widgets in UiBinder itself.

For eg.

Also added in *.gwt.xml

Then I try this in Selenium test case

WebElement element = driver.findElement(By.id("gwt-debug-loginButton"));

Sometimes it works correctly. But some times it throws the following exception,

Unable to locate element: {"method":"id","selector":"gwt-debug-loginButton"} Command duration or timeout: 62 milliseconds

What i need to update? Can anyone help me?


Solution

  • Use WebDriverWait, to search for element after a certain period of time. Something like this.

    try {
            (new WebDriverWait(driver, seconds, delay)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    try {
                        WebElement el = d.findElement(By.id("gwt-debug-loginButton"));
                        return true;
                    } catch (Exception e) {
                        return false;
                    }
                }
            });
        } catch (TimeoutException t) {
            //Element not found during the period of time
        }