java - Bootstrap Popover 的 Selenium ElementNotVisibleException

标签 java twitter-bootstrap selenium acceptance-testing

我正在使用 Selenium 2.37.1 和 Chrome 驱动程序 2.7(在 Ubuntu 13.10 上使用 Chrome 31.0.1650.63)为我的基于 Java/Spring/Boostrap 的网站编写一些测试。

测试点击登录文本,然后在点击提交按钮之前尝试将提供的用户名和密码输入到表单中。我已经分解了元素的查找并调用 sendKeys 来确定 Selenium 的后续位置(它在 username.sendKeys 上)

public void login(final String user) {
    webDriver.findElement(By.id("login")).click();
    final WebElement loginForm = webDriver.findElement(By.id("loginForm"));
    final WebElement username = loginForm.findElement(By.id("username"));
    username.sendKeys(user);
    final WebElement password = loginForm.findElement(By.id("password"));
    password.sendKeys(dslConstants.PASSWORD);
    loginForm.submit();
}

成功找到用户名,但是当我运行 username.isDisplayed() 时它实际上正在显示。

enter image description here

我假设这与 Selenium 无法正确处理 Bootstrap 弹出窗口有关,想知道是否有人对此有任何修复?

提前致谢。

编辑: loginForm.isDisplayed() 在调试时可见时也会返回 false。我也尝试添加一个等待条件,这也没有解决问题。

final WebDriverWait wait = new WebDriverWait(webDriver, TIME_OUT_IN_SECONDS);
wait.until(ExpectedConditions.visibilityOf(webDriver.findElement(By.id("username"))));

编辑2: 这是页面的 HTML/JSTL 方面。

<div class="navbar">
        <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <sec:authorize ifAnyGranted="ROLE_ANONYMOUS">
                <div class="hide" id="login-popover">
                    <form role="form" id="loginForm" method="post" action="j_spring_security_check">
                        <div class="control-group">
                            <label class="control-label" for="username">Username</label>
                            <div class="controls">
                                <input type="text" id="username" class="form-control" name="j_username" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="password">Password</label>
                            <div class="controls">
                                <input type="password" id="password" class="form-control" name="j_password" />
                            </div>
                        </div>
                        &nbsp;
                        <div class="control-group">
                            <div class="controls">
                                <button class="btn btn-primary" type="submit">Login</button>
                            </div>
                        </div>
                    </form>
                </div>
                <li><a href="#" id="login">Login</a></li>
                <li><a href="/accounts/register">Register</a></li>
            </sec:authorize>
        </ul>
    </div>

编辑 3: 使用 FireFox 25 时也会出现这种情况

最佳答案

尝试移动到 popover 元素以使其可见,然后再与之交互。像这样的东西:

    WebElement element = webDriver.findElement(By.id("login-popover"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element).build().perform();

然后执行等待序列。如果您必须单击该元素才能使其出现,那么您可以在 build() 之前执行此操作。

关于java - Bootstrap Popover 的 Selenium ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733957/

相关文章:

java - Spring 中的 ResponseEntity 和 HttpEntity 有什么区别?

java - Java数据类型到Typescript类型

javascript - 数据切换和数据目标未按预期工作

javascript - jQuery,单击其可单击容器内的元素 - 如何覆盖

wordpress - 在 Bootstrap3 中,Modal Popup 不会出现在叠加层的顶部。如何解决这个问题

python - 是否存在无法通过 Selenium 自动化的页面?

testing - 在 hudson 中控制 Selenium 测试速度

c# - 如何在 Bouncy CaSTLe 中实现 OpenSSL 的 EC_POINT_set_compressed_coordinates_GFp?

java - Android:looper/handler vs. Java Observer?

node.js - 带 Protractor 的 Sauce Labs,URI 格式错误