Python 无法单击特定列的链接
我正在尝试单击某个版本的“还原”超链接,但不起作用。我怎样才能让它工作?任何帮助都将不胜感激 最后一列元素:Python 无法单击特定列的链接,python,selenium-webdriver,Python,Selenium Webdriver,我正在尝试单击某个版本的“还原”超链接,但不起作用。我怎样才能让它工作?任何帮助都将不胜感激 最后一列元素: <tbody class="p-datatable-tbody"> <tr class="p-datatable-row" style="height: 28px;"> <td class="tbl-row"> <span style="color: rgba(27, 130, 215, 0.6);
<tbody class="p-datatable-tbody">
<tr class="p-datatable-row" style="height: 28px;">
<td class="tbl-row">
<span style="color: rgba(27, 130, 215, 0.6);">
<a href="/specification-management/versions/archived?org=Einstein&specName=Edit Specification name&version=2.3&documentSpecId=5dd5d19d554027001896f78b">2.3</a>
</span>
</td>
<td class="tbl-row">2019-11-20T23:52:18.027Z</td>
<td class="tbl-row">admin</td>
<td class="tbl-row"></td>
<td class="tbl-row">
<span style="color: rgb(27, 130, 215); text-decoration: underline; cursor: pointer;">RESTORE</span>
</td>
</tr>
</tbody>
方法:
def click_restore_link(self, version):
history_table = self.driver.find_element(*CommonLocators.DATA_TABLE)
history_table_row = history_table.find_elements(*CommonLocators.DATA_TABLE_ROWS)
for row in history_table_row:
table_columns = row.find_elements(*CommonLocators.DATA_TABLE_COLUMNS)
spec_version = table_columns[0]
spec_action = table_columns[4]
if version in spec_version.text:
spec_action.click()
代码:版本历史。单击恢复链接('2.3')
使用XPath,您可以创建一个定位器,根据包含的文本查找元素。例如,我们可以找到包含版本2.3的TR,然后在该行中找到还原链接 根据您发布的HTML,您可以将2.3的定位器编写为
//table//tr[.//a[.='2.3']]//span[.='RESTORE']
^ start with the table
^ that contains a TR
^ and that TR contains an A with the text '2.3'
^ and from that TR find the SPAN that contains the text 'RESTORE'
有了这些信息,您的方法将成为
def click_restore_link(self, version):
self.driver.find_element_by_xpath("//table//tr[.//a[.='" + version + "']]//span[.='RESTORE']").click()
您能否发布为
CommonLocator.DATA\u TABLE\u列提供的实际位置?我的初步猜测是,它可能返回td
元素,而不是span
您是对的,定位器设置为td。我尝试使用span&td span并出现“列表索引超出范围”错误。请编辑您的问题并添加表格的HTML。。。至少有代表性的一行。您尚未共享任何定位器。。。一切都是一个变量,没有一个在您发布的代码中定义。
def click_restore_link(self, version):
self.driver.find_element_by_xpath("//table//tr[.//a[.='" + version + "']]//span[.='RESTORE']").click()