Skip to main content
3 of 5
added 7 characters in body

Selecting WebElement with specific text from variable

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.