假设我们有一张人桌。每一行都有一张图片、一个名字、一个城市和一些社交按钮,比如 PM、加为好友、关注。
如何“加为好友”某个人?有 10 个人,有 10 个“添加为朋友”按钮,我有一个名字 - 比如说 George Busch。
我不知道他的个人资料将从这 10 个位置中取出,当页面生成时,我只想单击离 George Busch 最近的“添加为朋友”链接。
编辑:我将使用接受的答案提供的示例代码来演示我的案例:
<table id="addfriendstable" class="colouredgrid">
<tr id="person65894" class="resultsrow odd">
<td>George Busch</td>
<td><button class="befriend">Add as friend</button></td>
</tr>
<tr id="person3244" class="resultsrow even">
<td>Jimmy Corker</td>
<td><button class="befriend">Add as friend</button></td>
</tr>
<tr id="person91236" class="resultsrow odd">
<td>Abraham Linton</td>
<td><button class="befriend">Add as friend</button></td>
</tr>
</table>
编辑:这是我现实生活中的例子。我无法正确连接 xpath
这些是用户的可点击名称 xpath:
xpath=(//a[contains(text(),'Justin Timberlake')])[2]
//div[@id='main']/div/section/section/ul/li[2]/article/section/section/a
xpath=(//a[contains(@href, '/users/justin-timberlake')])[4]
//li[2]/article/section/section/a
这些是“添加为朋友”按钮的不同变体:
xpath=(//a[contains(text(),'Add as Friend')])[2]
//div[@id='main']/div/section/section/ul/li[2]/article/section/section/div/a
//a[contains(@href, '/users/1/friendships?friend_id=51c027d7f325db7ee7000032&origin=search')]
//li[2]/article/section/section/div/a