user3528733
user3528733

Reputation: 1299

Selenium, Java- finding table by class (without ID)

I have something like this:

  <table class="table">
    <thead>
    <tr>
        <th>Role</th>
        <th>Description</th>
        <th>Access</th>
        <th>Grant</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="role in roles">
        <td>{{role.role_name}}</td>
        <td>{{role.role_description}}</td>
        <td><input type="checkbox" ng-model="role.allow_access" ng-change="updateAllow(role)" ng-disabled="!(role.grantor_allow_grant == 'Y' || haveAllAdminRoles)" ng-true-value="'Y'" ng-false-value="'N'"></td>
        <td><input type="checkbox" ng-model="role.allow_grant" ng-change="updateAllow(role)"  ng-disabled="!(role.grantor_allow_grant == 'Y' || haveAllAdminRoles)" ng-true-value="'Y'" ng-false-value="'N'"></td>
    </tr>
    </tbody>
</table>

How I can find this table using WebDriver or WebConnector ? I tried sth like this:

WebElement table = driver.findElement(By.className("table"));

and it doesn't work, I received error:

org.openqa.selenium.NoSuchElementException: Unable to locate element:

Thanks for any help.

Upvotes: 0

Views: 745

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

May be when you are going to finding table, it could not be visible on the page due slow internet or other reason. To make sure table visible on the page try to find using WebDriverWait to wait until table visible as below :-

WebDriverwait wait = new WebDriverwait(driver, 10);
WebElement table = wait.until(Expectedconditions.visibilityOfElementLocated(By.className("table")));

Hope it helps..:)

Upvotes: 1

Guy
Guy

Reputation: 50809

That might be due to timing issue. You can use explicit wait to wait for the table to load or to be visible

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement table = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("table")));
// or
WebElement table = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("table")));

Upvotes: 1

Related Questions