1

I have the code below for my HTML, but my problem is selecting the specific WebElement (th) for my automation to click.

<div id="datatable-buttons_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"><div class="dt-buttons btn-group"><a class="btn btn-default buttons-selected btn-primary disabled" tabindex="0" aria-controls="datatable-buttons"><span><i class="fa fa-edit"></i> Update Asset Details</span></a><a class="btn btn-default buttons-selected btn-primary disabled" tabindex="0" aria-controls="datatable-buttons"><span><i class="fa fa-hand-o-up"></i> Assign Asset</span></a><a class="btn btn-default btn-primary" tabindex="0" aria-controls="datatable-buttons"><span><i class="fa fa-file-excel-o"></i> Export Spreadsheet</span></a></div><table id="datatable-buttons" width="100%" class="viewAssetTable table table-striped table-bordered dataTable no-footer" role="grid" aria-describedby="datatable-buttons_info">
    <thead>
    <tr role="row"><th width="10%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Asset No.  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=asset_number&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=asset_number&amp;direction=asc" class="data-reload">Asset No. <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Asset Type  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=AssetType.asset_type_name&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=AssetType.asset_type_name&amp;direction=asc" class="data-reload">Asset Type <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Model  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=model&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=model&amp;direction=asc" class="data-reload">Model <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Serial No.  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=serial_num&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=serial_num&amp;direction=asc" class="data-reload">Serial No. <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Bundle No.  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=bundle_asset_id&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=bundle_asset_id&amp;direction=asc" class="data-reload">Bundle No. <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Status  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=AssetStatus.asset_status_name&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=AssetStatus.asset_status_name&amp;direction=asc" class="data-reload">Status <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Status Effective Date  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=status_effectivity_date&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=status_effectivity_date&amp;direction=asc" class="data-reload">Status Effective Date <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="User  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=Employee.employee_name&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=Employee.employee_name&amp;direction=asc" class="data-reload">User <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Location  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=Location.location_name&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=Location.location_name&amp;direction=asc" class="data-reload">Location <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Location Remarks  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=location_remarks&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=location_remarks&amp;direction=asc" class="data-reload">Location Remarks <i class="fa fa-sort"> </i></a></th><th width="9%" class="sorting_disabled" rowspan="1" colspan="1" aria-label="Last Inventory  "><a data-url="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=last_audit_date&amp;direction=asc" href="/basket_uat/view-asset/search-asset?poNum=&amp;controlNum=&amp;bundleNum=&amp;warrantyEndDate=&amp;acquisitionDate=&amp;employeeName=&amp;salesInvoice=&amp;deliveryReceipt=&amp;model=&amp;serialNum=&amp;marketCircle=&amp;project=&amp;page=3&amp;sort=last_audit_date&amp;direction=asc" class="data-reload">Last Inventory <i class="fa fa-sort"> </i></a></th><th class="hide sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" aria-label=": activate to sort column ascending"></th><th class="hide sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" aria-label=": activate to sort column ascending"></th></tr>
    </thead>
     <tbody>

 <tr role="row" class="odd">
    <th scope="row" class="asset_number td-clickable">AN20160909191412</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">21</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="even">
    <th scope="row" class="asset_number td-clickable">AN20160909191722</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">22</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="odd">
    <th scope="row" class="asset_number td-clickable">AN20160909192932</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">23</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="even">
    <th scope="row" class="asset_number td-clickable">AN20160913103059</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">24</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="odd">
    <th scope="row" class="asset_number td-clickable">AN20160913104139</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">25</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="even">
    <th scope="row" class="asset_number td-clickable">AN20160913110714</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">26</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="odd">
    <th scope="row" class="asset_number td-clickable">AN20160913110915</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">27</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="even">
    <th scope="row" class="asset_number td-clickable">AN20160913111125</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">28</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="odd">
    <th scope="row" class="asset_number td-clickable">AN20160913111946</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">29</td>
    <td class="hide can_be_parent">1</td>
</tr><tr role="row" class="even">
    <th scope="row" class="asset_number td-clickable">AN20160913114152</th>
    <td>Desktop</td>
    <td>Dell 0604</td>
    <td>SN60609072016</td>
    <td class="bundle_asset_id td-clickable"></td>
    <td class="asset_status ">Assigned Permanent</td>
    <td class="status_effectivity td-clickable">2016-09-10</td>
    <td class="asset_user td-clickable">Admin Manager</td>
    <td class="asset_location td-clickable">PICUP4FCA</td>
    <td>Best</td>
    <td class="asset_inventory td-clickable">2016-09-12</td>
    <td class="hide asset_id">30</td>
    <td class="hide can_be_parent">1</td>
</tr></tbody>

Here is what I have tried so far:

String selectedAsset = dataTable.getData("AssetNumberToUpdate");
WebElement selectedAssetWE = driver.findElement(By.xpath("//th[contains(.,'" + selectedAsset + "')]"));
selectedAssetWE.click();

I have no problem clicking other web element in my automation but with this scenario I can't really seems to click it. I have a feeling that my problem lies in my Xpath. I'm not quiet experienced in Xpath.

UPDATE: The "AssetNumberToUpdate" is basically the column of my excel file where my specific TH's text is stored.

  • You have not provided all the info, where is this "AssetNumberToUpdate"? – Yu Zhang Sep 15 '16 at 21:19
  • Hi @YuZhang, thanks for the comment. Please see my updates. Thanks. – RyanskyHeisenberg Sep 19 '16 at 6:03
2

Generic answer to figure out this and any future XPath locators you might struggle with:

  1. Debug your code to get the fully constructed XPath
  2. Open your test page with Chrome and open Developer Tools (F12)
  3. In the console, type $x("your xpath here")
  4. Check the results to see if it leads to the correct element.
  5. If not, adjust Xpath until it does.

Free protip: you can use the same technique for Css locators using $$().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.