![](/img/trans.png)
[英]Select a Checkbox in a Table If Row Includes X Value with Selenium Python
[英]how to select a checkbox in a table depending on the row value?
我正在與一個GWT應用程序自動化。 有一個帶有以下列的表:復選框,用戶ID,用戶名,Fname,LName,電子郵件。
這是表中單行的代碼:
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="8902" align="right" style="height: 19px; width: 34px;">
<div style="height: 19px; width: 34px;">
<div class="gwt-HTML">8902</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="Agency_Group_0_Agency_Group_Member_0_dan_all_features_6" align="left" style="height: 19px; width: 146px;">
<div style="height: 19px; width: 146px;">
<div class="gwt-HTML">Agency_Group_0_Agency_Group_Member_0_dan_all_features_6</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="" align="left" style="height: 19px; width: 63px;">
<div style="height: 19px; width: 63px;">
<div class="gwt-HTML">
</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="" align="left" style="height: 19px; width: 28px;">
<div style="height: 19px; width: 28px;">
<div class="gwt-HTML">
</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="Agency_Group_0_Agency_Group_Member_0_dan_all_features_6@xxx.com" align="left" style="height: 19px; width: 146px;">
<div style="height: 19px; width: 146px;">
<div class="gwt-HTML">Agency_Group_0_Agency_Group_Member_0_dan_all_features_6@xxx.com</div>
</div>
</td>
</tr>
</tbody>
</table>
復選框的xpath為:/ html / body / div [6] / div / table / tbody / tr [2] / td [2] / div / div / div / div / table / tbody / tr [2] / td / div / div / div / div / div [1] / div / table [10] / tbody / tr / td [1]
用戶名列的xpath為:/ html / body / div [6] / div / table / tbody / tr [2] / td [2] / div / div / div / div / table / tbody / tr [2] / td / div / div / div / div [1] / div / table [10] / tbody / tr / td [3]
我的代碼遍歷了一個名稱列表,應該單擊與該名稱關聯的復選框:對於成員列表中的名稱:打印“與%s關聯的單擊復選框”%name chrome.find_element_by_xpath(“ // tr [td [contains( text()='“ +名稱+”']]] / td [1]“)。click()
我收到的錯誤是:selenium.common.exceptions.InvalidSelectorException:消息:無效的選擇器:無法使用xpath表達式定位元素// tr [td [contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]] / td [1]由於以下錯誤:語法錯誤:無法在'Document'上執行'evaluate':字符串'// tr [td [contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]] / td [1]'為不是有效的XPath表達式。 (會話信息:chrome = 48.0.2564.116)
我的問題是:如何單擊與名稱關聯的復選框?
您沒有正確使用contains()
。 更換:
//tr[td[contains(text()='" + name + "')]]/td[1]
與:
//tr[td[contains(text(), '" + name + "')]]/td[1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.