I have a html element like so:
<table>
<tbody>
<tr>
<td>My Text</td>
<td>Some Other Text</td>
<td><img src="../delete.png" alt="trashcan"></td>
</tr>
<tr>
<td>More Text</td>
<td>Some More Text</td>
<td><img src="../delete.png" alt="trashcan"></td>
</tr>
</tbody>
</table>
I'm want to find a table row by text and then click the trashcan icon to delete it.
So my idea is to loop over the rows <tr/>
and then loop over the cells <td/>
. If the text matches the cell text, find the image from that row by XPATH
and then click it.
tbody = driver.find_element_by_tag_name("tbody")
tr = tbody.find_elements_by_tag_name("tr")
for row in tr:
td = row.find_elements_by_tag_name("td")
print(len(td))
for cell in td:
if cell.text == "More Text":
delete = row.find_element_by_xpath('//img[@alt="trashcan"]')
delete.click()
My understanding is that driver
is the entire page. tbody
is just the tbody element in that page. So whatever I try to locate from there has to be a child of that element. To confirm I print out the length of the td
elements which is prints out "3".
The delete button delete = row.find_element_by_xpath('//img[@alt="trashcan"]')
selects the delete button from the first table row instead of from the second row.
I also tried
delete = row.find_elements_by_xpath('//img[@alt="trashcan"]')
delete[0].click()
But it also selects the row.
Just to be sure I printed out the row (print(row.get_attribute("innerHTML"))
in the if condition and it prints out the second row.
Any ideas what's going on or how I could select the img
in the second row instead?