由於某種原因,單擊鏈接似乎在Selenium WebDriver中對我無效,而在Selenium IDE中它工作得很好。Selenium Firefox Webdriver NoSuchElement鏈接上的異常:
我在Selenium IDE和Selenium Webdriver上構建的所有其他「複製粘貼」的東西似乎都起作用。
例外:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"}
Command duration or timeout: 10.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.50.1', revision: 'd7fc91b', time: '2016-01-29 19:04:49'
System info: host: 'LT1736', ip: '10.54.3.175', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_71'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0.4, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 62532082-d8c1-4d22-8884-6e266449ca06
*** Element info: {Using=css selector, value=a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3}
代碼:
public class CRUD_Technologie {
public WebDriver driver;
@Test(priority=1)
public void openApp() {
driver.get("http://localhost:8080/index.html");
}
@Test(priority=2)
public void login() {
driver.findElement(By.cssSelector("#usernameInput")).sendKeys("MxAdmin");
driver.findElement(By.cssSelector("#passwordInput")).sendKeys("1");
driver.findElement(By.cssSelector("#loginButton")).click();
}
@Test(priority=3)
public void selectUserAndGoToPageTechnologieOverview() {
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
driver.findElement(By.cssSelector("div.mx-demouserswitcher-toggle")).click();
driver.findElement(By.cssSelector("li[data-id='3']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"))).click();
}
@Parameters("browser")
@BeforeTest
public void beforeTest(String browser) {
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}
else {
driver = new HtmlUnitDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterTest
public void afterTest() {
driver.close();
}
}
鏈接元素的相關HTML:
<div id="mxui_widget_Navbar_0" class="navbar navbar-default mx-navbar mx-name-menuBar1 hidden-xs" tabindex="-1" data-mendix-id="150" style="background-color: black ! important;" widgetid="mxui_widget_Navbar_0">
<ul class="nav navbar-nav">
<li class="mx-navbar-item">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-0">
<span class="glyphicon glyphicon-home"/>
Home
</a>
</li>
<li class="mx-navbar-item dropdown">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1">
<span class="glyphicon glyphicon-th-list"/>
Master Data Mendix
<b class="caret"/>
</a>
<ul class="dropdown-menu mx-navbar-submenu">
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-0">
<span class="glyphicon glyphicon-euro"/>
Rollen
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-1">
<span class="glyphicon glyphicon-modal-window"/>
Scherm type
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-2" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-2">
<span class="glyphicon glyphicon-book"/>
Story type
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-3">
<span class="glyphicon glyphicon-briefcase"/>
Technologie
</a>
</li>
</ul>
</li>
<li class="mx-navbar-item dropdown">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2">
<span class="glyphicon glyphicon-th-list"/>
Master Data AFAS
<b class="caret"/>
</a>
<ul class="dropdown-menu mx-navbar-submenu">
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2-0">
<span class="glyphicon glyphicon-glass"/>
Klanten
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2-1">
<span class="glyphicon glyphicon-list-alt"/>
Werkmaatschappij
</a>
</li>
</ul>
</li>
</ul>
</div>
請發表的「李[數據-ID = '3' 失敗元素 –
是否點擊的HTML代碼]「觸發另一個AJAX?如果是這樣,你需要添加隱含的等待到最後一個「a.mx-name-159b6aef ...」 反正......那是失敗的元素:) –
你的類名a.mx-name-159b6aef-dbc3- 5b23-a735-cf99f8341771-1-3似乎是動態的。當硒創建一個新的會話,並打開一個網頁時,你的CSS可能會改變。嘗試使用另一個定位器策略(xpath,linktext或更好的css定位器)。 –