Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading