![](/img/trans.png)
[英]Selenium ElementNotVisibleException is an instance of InvalidStateException
[英]Selenium ElementNotVisibleException for Bootstrap Popover
我正在使用Selenium 2.37.1和Chrome Driver 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()
成功,但是當我運行username.isDisplayed()
時,它實際上已顯示出來。
我假設這與Selenium無法正確處理Bootstrap彈出窗口有關,並且想知道是否有人對此進行了修復?
提前致謝。
編輯: loginForm.isDisplayed()
在調試時可見時也返回false。 我還嘗試添加等待條件,但這也不能解決問題。
final WebDriverWait wait = new WebDriverWait(webDriver, TIME_OUT_IN_SECONDS);
wait.until(ExpectedConditions.visibilityOf(webDriver.findElement(By.id("username"))));
Edit2:這是頁面的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>
<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()之前執行此操作。
我設法解決了這個問題。 在調試和檢查網頁時(使用Chrome),我注意到實際上有兩種登錄表單。
我可以通過在另一個文件中(或在單擊“登錄”鏈接時執行的Javascript中)聲明登錄表單html來解決此問題。 無論哪種方式,通過執行webDriver.findElement(By.class(“ popover-content”))然后在其中搜索表單都可以解決我的問題。
public void login(final String user) {
webDriver.findElement(By.id("login")).click();
final WebElement loginForm = webDriver.findElement(By.class("popover-content)).findElement(By.id("loginForm"));
loginForm.findElement(By.id("username")).sendKeys(user);
loginForm.findElement(By.id("password"))password.sendKeys(dslConstants.PASSWORD);
loginForm.submit();
}
我想從中得到的教訓是,我回顧了在Web應用程序中使用的庫的實際工作方式!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.