Reputation: 129
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 that I need to identify the select tag after the correct table as well.
Can anyone help me out with this Code snippet?
Upvotes: 2
Views: 2149
Reputation: 29362
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.
Upvotes: 2