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

Time:11-29

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  cellspacing="0" id="DataTables_Table_6" role="grid">
      <thead>
         <tr role="row"><th style="text-align: left; width: 167.104px;"  rowspan="1" colspan="1"><div ><span ></span>Parent Instance ID</div></th><th style="text-align: left; width: 116.917px;"  rowspan="1" colspan="1"><div ><span ></span>Instance ID</div></th><th style="text-align: left; width: 97.1771px;"  rowspan="1" colspan="1"><div ><span ></span>Plan Name</div></th><th style="text-align: left; width: 168.719px;"  rowspan="1" colspan="1"><div ><span ></span>Client Defined Identifier</div></th><th style="text-align: left; width: 39.5729px;"  rowspan="1" colspan="1"><div ><span ></span>Units</div></th><th style="text-align: left; width: 89.8438px;"  rowspan="1" colspan="1"><div ><span ></span>Status</div></th></tr>
      </thead>
      
<tbody> 
   <tr role="row" >
  <td style="text-align: left;"><span style="padding-left:px;\"><a href="#"  ></a></span></td>
  <td style="text-align: left;"><span style="padding-left:px;\"><a href="#"  ">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".

CodePudding user response:

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.

  • Related