我正在使用Selenium和Python。我有一张桌子。我使用这个Xpath找到了包含文本'Honorable'的行: -
element = driver.find_element_by_xpath("//*[contains(., 'Honorable')]")
现在我需要点击具有以下HTML的编辑按钮(取自Chrome的Inspector工具)
<a id="prefixmasterListForm:prefixMasterTable:0:editLink
完整的HTML是:
<tr class="rich-table-row rich-table-firstrow ">
<td class="rich-table-cell " id="prefixmasterListForm:prefixMasterTable:0:j_id347" width="auto">
Honorable
</td>
<td class="rich-table-cell alignCenter" id="prefixmasterListForm:prefixMasterTable:0:j_id350" width="auto">
<img id="prefixmasterListForm:prefixMasterTable:0:activeic" src="images/yes.gif" alt="active">
</td>
<td class="rich-table-cell " id="prefixmasterListForm:prefixMasterTable:0:j_id352" width="auto">
General Prefix
</td>
<td class="rich-table-cell " id="prefixmasterListForm:prefixMasterTable:0:j_id360" width="auto">
MALE
</td>
<td class="rich-table-cell " id="prefixmasterListForm:prefixMasterTable:0:column6" width="auto">
<a id="prefixmasterListForm:prefixMasterTable:0:editLink" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['prefixmasterListForm'],'prefixmasterListForm:prefixMasterTable:0:editLink,prefixmasterListForm:prefixMasterTable:0:editLink,prefixMasterId,15661602','');}return false">
<img src="images/iconedit.gif" alt="Edit" class="image">
</a>
<a id="prefixmasterListForm:prefixMasterTable:0:deleteLink" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['prefixmasterListForm'],'prefixmasterListForm:prefixMasterTable:0:deleteLink,prefixmasterListForm:prefixMasterTable:0:deleteLink,prefixMasterId,15661602,prefixMasterName,Honorable','');}return false">
<img src="images/icondelete.gif" alt="delete" class="image">
</a>
</td>
</tr>
如何使用XPath获取Edit链接元素?
根据Arran的帮助,我还检查了'Honorable'旁边的列有一个alt =无效的图像
"//td[normalize-space(text())='Honorable']/following-sibling::td[img[@alt='inactive']]"
答案 0 :(得分:1)
尝试://a[@id='prefixmasterListForm:prefixMasterTable:0:editLink']
P.S.如果你想用xpath验证或做一些简单的工作,你可以安装 FF + FireBug + FirePath
答案 1 :(得分:1)
简单://tr[td[contains(., 'Honorable')]]/td/a[contains(@id, ':editLink)]
- 确切地说,A
中包含的TD
中包含的TR
也包含TD
,其中包含字符串“Honorable ”
答案 2 :(得分:1)
//td[normalize-space(text())='Honorable']/following-sibling::td//a[.//img[@alt='Edit']]
td
text
等于到'尊敬',td
跟随a
,内有image
它,其中alt
的{{1}}属性设置为'编辑'