我有点沮丧,因为我在
<table>
HTML、by.XPATH
和 following-sibling
中搜索了 Web 元素。但系统无法定位我的元素。
我想将标记为“[电子邮件受保护]”的电子邮件地址的下拉值从“标准”更改为“管理员”。
但是属性
ID
的值“角色”并不是唯一的。没有唯一的属性来定位元素。
表格看起来像这样(没有我可以搜索的唯一 ID 或名称:
<tbody> <tr id="0dca86a5-13d3-4ab5-a2e6-e75ab381dca9">
<td class="emailAddress">[email protected]</td>
<td class="role">
<form action="/echo/UserManagement/UpdateRole?userId=0dca86a5-13d3-4ab5-a2e6-e75ab381dca9&accountReference=4711&username=max%40max.com" method="post" novalidate="novalidate">
<div class="field-inline">
<select class="roles medium" id="role" name="role"><option value="Admin">Admin</option>
<option value="Standard">Standard</option>
<option selected="selected" value="Basic">Basic</option>
<option value="Template">Template</option>
</select>
</div>
<div class="field-inline">
<button class="button button-primary save" type="submit">Save</button>
<noscript>
<button class="button button-primary" type="submit">Save</button>
</noscript>
</div>
</form>
</td>
<td class="action">
<form action="/echo/UserManagement/RemoveUserFromAccount" method="post" novalidate="novalidate"><input id="userId" name="userId" type="hidden" value="0a23788b-0ed6-401b-a11e-8004d34acf1a"><input id="usernameToRemove" name="usernameToRemove" type="hidden" value="[email protected]">
<input id="accountReference" name="accountReference" type="hidden" value="4711">
<button type="submit" class="button button-danger remove_user" data-confirm="true">Remove</button>
</form>
</td> </tr> <tr id="e5b128b5-75f9-4002-bcd9-11c8bc891d49">
<td class="emailAddress">[email protected]</td>
<td class="role">
<form action="/echo/UserManagement/UpdateRole?userId=e5b128b5-75f9-4002-bcd9-11c8bc891d49&accountReference=4711&username=john%40john.com" method="post" novalidate="novalidate">
<div class="field-inline">
<select class="roles medium" id="role" name="role"><option selected="selected" value="Admin">Admin</option>
<option value="Standard">Standard</option>
<option value="Basic">Basic</option>
<option value="Template">Template</option>
</select>
</div>
<div class="field-inline">
<button class="button button-primary save" type="submit">Save</button>
<noscript>
<button class="button button-primary" type="submit">Save</button>
</noscript>
</div>
</form>
</td>
<td class="action">
<form action="/echo/UserManagement/RemoveUserFromAccount" method="post" novalidate="novalidate"><input id="userId" name="userId" type="hidden" value="f45a18fc-4903-4189-8cab-c158630373a3"><input id="usernameToRemove" name="usernameToRemove" type="hidden" value="[email protected]">
<input id="accountReference" name="accountReference" type="hidden" value="4711">
<button type="submit" class="button button-danger remove_user" data-confirm="true">Remove</button>
</form>
</td> </tr> <tr id="5e356ca7-fa23-45a9-b7f3-c9e71ce5cb5f">
<td class="emailAddress">[email protected]</td>
<td class="role">
<form action="/echo/UserManagement/UpdateRole?userId=5e356ca7-fa23-45a9-b7f3-c9e71ce5cb5f&accountReference=4711&username=katja%40katja.com" method="post" novalidate="novalidate">
<div class="field-inline">
<select class="roles medium" id="role" name="role"><option value="Admin">Admin</option>
<option selected="selected" value="Standard">Standard</option>
<option value="Basic">Basic</option>
<option value="Template">Template</option>
</select>
</div>
<div class="field-inline">
<button class="button button-primary save" type="submit">Save</button>
<noscript>
<button class="button button-primary" type="submit">Save</button>
</noscript>
</div>
</form>
</td>
<td class="action">
<form action="/echo/UserManagement/RemoveUserFromAccount" method="post" novalidate="novalidate"><input id="userId" name="userId" type="hidden" value="1c094ceb-13a7-41cf-b754-921ac15dd9dc"><input id="usernameToRemove" name="usernameToRemove" type="hidden" value="[email protected]">
<input id="accountReference" name="accountReference" type="hidden" value="4711">
<button type="submit" class="button button-danger remove_user" data-confirm="true">Remove</button>
</form>
</td> </tr> </tbody>
我的代码试验:
driver.find_element(By.XPATH, "//td[text()='[email protected]']/following-sibling::/form/select")
和
driver.find_element(By.XPATH, "//td[text()='[email protected]']/following-sibling::*[1]/form/select")
尝试以下 XPath 表达式:
(//form[contains(@action,'katja.com')]//following::select)[1]
说明: XPath 表达式选择任何
<select>
元素(其操作属性包含字符串“katja.com”)后面的第一个 <form>
元素。
代码应如下所示:
driver.find_element(By.XPATH, "(//form[contains(@action,'katja.com')]//following::select)[1]"))