Gwt 如何克服Selenium中的元素id异常?

Gwt 如何克服Selenium中的元素id异常?,gwt,selenium,selenium-webdriver,gwt2,selenium-server,Gwt,Selenium,Selenium Webdriver,Gwt2,Selenium Server,T在UiBinder本身中为GWT小部件设置“id” 例如, 还在*.gwt.xml中添加了 然后我在Selenium测试用例中尝试这个 WebElement element = driver.findElement(By.id("gwt-debug-loginButton")); 有时它工作正常。但有时它会抛出以下异常 找不到元素: {“方法”:“id”,“选择器”:“gwt调试登录按钮”}命令持续时间或 超时时间:62毫秒 我需要更新什么? 有人能帮我吗?使用WebDriverWait,在

T在UiBinder本身中为GWT小部件设置“id”

例如,

还在*.gwt.xml中添加了

然后我在Selenium测试用例中尝试这个

WebElement element = driver.findElement(By.id("gwt-debug-loginButton"));
有时它工作正常。但有时它会抛出以下异常

找不到元素: {“方法”:“id”,“选择器”:“gwt调试登录按钮”}命令持续时间或 超时时间:62毫秒

我需要更新什么?
有人能帮我吗?

使用WebDriverWait,在一段时间后搜索元素。像这样的

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
    }
试试看{
(新WebDriverWait(驱动程序,秒,延迟))。直到(新的预期条件(){
公共布尔应用(WebDriver d){
试一试{
WebElement el=d.findElement(By.id(“gwt调试登录按钮”);
返回true;
}捕获(例外e){
返回false;
}
}
});
}捕获(超时异常t){
//在时间段内找不到元素
}

当您试图使用
selenium WebDriver查找网页上的任何元素时。
您可以使用或使
驱动程序等待页面完全加载

隐式等待的示例(此代码通常在初始化驱动程序后使用)—

如果驱动程序找不到您要查找的元素,上述语句会使驱动程序等待10秒钟。如果驱动程序在10秒后仍无法找到它,则驱动程序将抛出异常

显式等待的示例-在您的情况下,这专门用于单个
Web元素
-

new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("gwt-debug-loginButton")));
上述代码将使驱动程序等待20秒,直到找到该元素。如果它在20秒后仍然找不到元素,它将抛出TimeoutException。 您可以检查API的ExpectedCondition(这个类有许多有趣的变体)


注意仅当驱动程序找不到代码要查找的元素时,才会等待指定的时间段,如果驱动程序找到元素,则只需继续执行)

+1,用于指出默认情况下包含的预定义解决方案。
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("gwt-debug-loginButton")));