【问题标题】:How to click a button with Selenium if a cell in row contains certain text如果行中的单元格包含某些文本,如何使用 Selenium 单击按钮
【发布时间】:2022-08-18 15:50:44
【问题描述】:

如果某行中的单元格包含文本“Reconciliation”,我想使用 XPath 单击一行中的按钮。 请问如何才能做到这一点?非常感谢您提前。 以下是网站的html(我不能分享链接,因为它是一个内部网站):

<tbody>
<tr class=\"header\">
<th scope=\"col\" style=\"width:25px;\">&nbsp;</th>
<th scope=\"col\" style=\"width:25px;\">&nbsp;</th>
<th scope=\"col\" style=\"width:25px;\">&nbsp;</th>
<th align=\"center\" scope=\"col\" style=\"width:50px;\">ID</th>
<th align=\"center\" scope=\"col\" style=\"width:200px;\">Project</th>
<th align=\"center\" scope=\"col\" style=\"width:300px;\">Task</th>
<th align=\"center\" scope=\"col\" style=\"width:100px;\">Lock</th>
<th align=\"center\" scope=\"col\" style=\"width:100px;\">Person</th>
<th align=\"center\" scope=\"col\" style=\"width:100px;\">First Date</th>
<th align=\"center\" scope=\"col\" style=\"width:130px;\">Second Date</th>
</tr>
<tr>
<td><span><a href=\"/placeholder/DisplayTask.aspx?taskWflId=UADMERR2-PBIZCAL2-974f892e-eb51-42b7-bef6-6587ec7f76a2&amp;wklist=ADMIN&amp;prjId=12345&amp;prd=1\" target=\"_self\"><img src=\"/sth/open.gif\" border=\"0\" alt=\"Open this task\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessHistory.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/history.gif\" border=\"0\" alt=\"Show project history\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessStatus.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/roadmap.gif\" border=\"0\" alt=\"Show project roadmap\"></a></span></td>
<td><span><a href=\"Worklist.aspx?IssueSourceIds=12345\" target=\"_self\" style=\"text-decoration:none\">12345</a></span></td>
<td><span>\"Product name placeholder\"</span></td>
////Referenetial is in the line below
<td><span><span class=\"error\">Reconciliation Error</span><br>A technical error has occured.</span></td>
<td><span></span></td>
<td><span>ADMIN</span></td>
<td><span>01-08-2022</span></td>
<td><span>01-08-2022 13:26:07</span></td>
</tr>
<tr class=\"alternate\">
<td><span><a href=\"/placeholder/DisplayTask.aspx?taskWflId=UFUPPUB1-MLOTFUP1-332ce59b-7773-4098-9dd0-0a3ae790dea1&amp;wklist=FOLLOW_UP&amp;prjId=12345&amp;prd=1\" target=\"_self\"><img src=\"/sth/open.gif\" border=\"0\" alt=\"Open this task\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessHistory.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/history.gif\" border=\"0\" alt=\"Show project history\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessStatus.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/roadmap.gif\" border=\"0\" alt=\"Show project roadmap\"></a></span></td>
<td><span><a href=\"Worklist.aspx?IssueSourceIds=12345\" target=\"_self\" style=\"text-decoration:none\">12345</a></span></td>
<td><span>\"Product name placeholder\"</span></td>
<td><span><span class=\"followup\">Issue Follow Up</span><br>Summary &amp; Status.<br>Concerned issue: \"Product name placeholder\"</span></td>
<td><span></span></td>
<td><span>FOLLOW_UP</span></td>
<td><span>01-08-2022</span></td>
<td><span>29-07-2022 10:05:07</span></td>
</tr>
<tr>
<td><span><a href=\"/placeholder/DisplayTask.aspx?taskWflId=UFUPADM1-MLOTFUP1-d02d29e7-f2c7-4a1c-9313-62ff652c1aa4&amp;wklist=ADMIN&amp;prjId=12345&amp;prd=1\" target=\"_self\"><img src=\"/sth/open.gif\" border=\"0\" alt=\"Open this task\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessHistory.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/history.gif\" border=\"0\" alt=\"Show project history\"></a></span></td>
<td><span><a href=\"/placeholder/DisplayProcessStatus.aspx?prjId=12345&amp;prd=1\" target=\"_blank\"><img src=\"/sth/roadmap.gif\" border=\"0\" alt=\"Show project roadmap\"></a></span></td>
<td><span><a href=\"Worklist.aspx?IssueSourceIds=12345\" target=\"_self\" style=\"text-decoration:none\">12345</a></span></td>
<td><span>\"Product name placeholder\"</span></td>
<td><span><span class=\"followupAdmin\">Issue Follow Up ADMIN</span><br>Summary, Status &amp; Admin Tools.<br>Concerned issue: \"Product name placeholder\"</span></td>
<td><span></span></td>
<td><span>ADMIN</span></td>
<td><span>01-08-2022</span></td>
<td><span>29-07-2022 10:05:07</span></td>
</tr>
</tbody>
  • 我没有看到 tr[2] 中包含的文本 Reconciliation
  • 给您带来的不便,我深表歉意。我已经更新了问题
  • 是否可以分享页面的url
  • 你能试试我的答案,让我知道你得到了什么

标签: python selenium selenium-webdriver


【解决方案1】:

如果要通过文本的子字符串单击元素,可以使用text()选择器

下面为您提供包含文本 Reconciliation 的元素

element = driver.find_element(by=By.XPATH,value="//*[text()[contains(.,'Reconciliation')]]")
element.click()

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多