简体   繁体   中英

how to select a checkbox in a table depending on the row value?

I'm working with to automate a GWT app. There is a table with the following columns: checkbox, userID, Username, Fname, LName, Email.

Here's the code for a single row in the table:

</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>

The xpath to the checkbox is: /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[1]

The xpath to the username column is: /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]

My code loops through a list of names and it is supposed to click the checkbox associated with that name: for name in memberlist: print 'clicking checkbox associated with %s' %name chrome.find_element_by_xpath("//tr[td[contains(text()='" + name + "')]]/td[1]").click()

The error I'm getting is: selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //tr[td[contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]]/td[1] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//tr[td[contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]]/td[1]' is not a valid XPath expression. (Session info: chrome=48.0.2564.116)

My question is: How do I click the checkbox associated with the name?

You not using contains() correctly. Replace:

//tr[td[contains(text()='" + name + "')]]/td[1]

with:

//tr[td[contains(text(), '" + name + "')]]/td[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM