我尝试在表(Webelement - Selenium)中查找特定按钮,但该按钮没有 ID 或其他内容。有人可以给我提示如何做到这一点吗?
在此示例中,我想通过 [email protected]“单击”删除按钮 作为程序,我使用 VisualStudio。
表格看起来像这样:
<tr id="0dca11a5-11d3-4ab5-a255-e55ab331dc19">
<td class="emailAdress">[email protected]</td>
<td class="action">
<form action="RemoveUserFromAccount" method="post">
<input id="usernameToRemove" name="usernameToRemove" value="[email protected]">
<button type="submit" class="button button-danger remove_user">Delete</button>
</form>
</td>
</tr>
<tr id="e5b111b5-71e9-4272-bcd9-11c8fe831a49">
<td class="emailAdress">[email protected]</td>
<td class="action">
<form action="RemoveUserFromAccount" method="post">
<input id="usernameToRemove" name="usernameToRemove" value="[email protected]">
<button type="submit" class="button button-danger remove_user">Delete</button>
</form>
</td>
</tr>
<tr id="a50d080c-6ed1-480f-b92f-6576fe3e6549">
<td class="emailAdress">[email protected]</td>
<td class="action">
<form action="RemoveUserFromAccount" method="post">
<input id="usernameToRemove" name="usernameToRemove" value="[email protected]">
<button type="submit" class="button button-danger remove_user">Delete</button>
</form>
</td>
</tr>
感谢您的帮助 安迪
我尝试了这段代码,但没有解决任何问题:
button = driver.find_element(By.XPATH, "//td[.='[email protected]']")
或
button = driver.find_element(By.XPATH, "//*contains(text(),'[email protected]')]")
或
button = driver.find_element(By.XPATH("//*[text() = '[email protected]']"))
button.get_attribute('parentElement')
此 XPath 选择包含电子邮件地址
<td>
的 “[email protected]”
元素,然后移动到包含表单的下一个同级 <td>
,最后选择该表单中的 <button>
元素。
button = driver.find_element(By.XPATH, "//td[text()='[email protected]']/following-sibling::td/form/button")