Loop through multiple html tables looking for specific <td> values

Question:

I’m trying to find an account number in a table (it can be in many multiple tables) along with the status of the account. I’m trying to utilize find_element using the Xpath and the odd thing is that it is saying it cannot find it. You can see in the html that the id exists yet it is defaulting to my except saying table not found. My end result is to find the table that has the header Instance ID with the value of 9083495r3498q345 and to give the value under the Status field for that row in the same table. Please keep in mind that it may not be DataTables_Table_6 but could be DataTables_Table_i

<table class="data-table clear-both dataTable no-footer" cellspacing="0" id="DataTables_Table_6" role="grid">
      <thead>
         <tr role="row"><th style="text-align: left; width: 167.104px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Parent Instance ID</div></th><th style="text-align: left; width: 116.917px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Instance ID</div></th><th style="text-align: left; width: 97.1771px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Plan Name</div></th><th style="text-align: left; width: 168.719px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Client Defined Identifier</div></th><th style="text-align: left; width: 39.5729px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Units</div></th><th style="text-align: left; width: 89.8438px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Status</div></th></tr>
      </thead>
      
<tbody> 
   <tr role="row" class="odd">
  <td style="text-align: left;"><span style="padding-left:px;"><a href="#" class="doAccountsPanel" ></a></span></td>
  <td style="text-align: left;"><span style="padding-left:px;"><a href="#" class="doAccountsPanel" ">Not Needed</a></span></td>
  <td style="text-align: left;">The Product</td>
  <td style="text-align: left;">9083495r3498q345</td>
  <td style="text-align: left;">1</td>
  <td style="text-align: left;">Suspended</td>
</tr></tbody></table>
try:    
        driver_chrom.find_element(By.XPATH,'//*[@id="DataTables_Table_6"]')
        print("Found The Table")
except:
        print("Didn't find the table")

I would have expected my print result to be "Found the Table", but I’m getting the "Didn’t find the table".

Asked By: Ryan_The_Noob

||

Answers:

DataTables are dynamic elements – the actual info they hold is being hydrated by javascript on an empty table skeleton, after page loads. Therefore, you need to wait for the table to fully load, then look up the information it holds:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 20)
[...]
desired_info = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="DataTables_Table_6"]')))
print(desired_info.text)

See Selenium documentation here.

Answered By: Barry the Platipus
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.