链接上的Selenium Firefox Webdriver NoSuchElement异常:

时间:2016-02-04 15:55:51

标签: java selenium-webdriver testng

出于某种原因,点击链接似乎对我来说在Selenium WebDriver中无效,而在Selenium IDE中它可以正常工作。

所有其他"复制粘贴"我在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>

1 个答案:

答案 0 :(得分:1)

(1)首先,你没有等待与使用其他元素相同的元素

实施例: -

你在说: -

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li[data-id='3']"))).click();

你没有等待,尝试像在其余代码中那样做similer

driver.findElement(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3")).click();

(2)如果您能够使用相同的CSS定位器单击IDE,请尝试通过JavascriptExecutor单击。

JavascriptExecutor是Selenium Webdriver提供的界面

if (driver.findElements(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3").size() != 0) {
    WebElement element= driver.findElement(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"))
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
}
 else{
          System.out.println("Element not present");
     }

希望它会对你有所帮助:)。