I got some Problems to find the right Element using Selenium. The HTML Code looks like this:
<div>
<div>
<div>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div></div>
<div>
<div>
<div>
<div>
<label></label>
<label></label>
<select></select>
</div>
</div>
</div>
</div>
(Can’t publish the original URL due its company use only)
In the HTML Code there multiple (count changes every time) Tables looking like the Code above. To find the correct Table I am using this Code:
for index, table in enumerate(self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')):
if table.text == header_text: break
main_table = self.browser.find_elements(By.XPATH, f'//table')[index]
Works fine for me, the Problem is I need to identify the Select Tag after the correct Table as well. Can anyone help me out with this Code snippet? Thanks in advanced.
CodePudding user response:
self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')
this will return a list. you can go to select tag by using the below XPath:
list_of_th = self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')
select_web_element = list_of_th[0].find_element(By.XPATH, ".//ancestor::table/../../../following-sibling::div[2]/descendant::select")
should get the job done.