Python 如何使用Xpath for selenium选择锚定标记?

Python 如何使用Xpath for selenium选择锚定标记?,python,html,selenium,webautomation,Python,Html,Selenium,Webautomation,我正在使用Selenium实现web自动化,但无法选择具有onClick属性且没有href和类名的锚标记。下面是表单的源代码,我想在其中单击第一个和第三个锚定标记 <td colspan="4" class="leftTopFormLabel"> 12. Details of Nominee :<span class="mandatoryField">*</span> </td>

我正在使用Selenium实现web自动化,但无法选择具有onClick属性且没有href和类名的锚标记。下面是表单的源代码,我想在其中单击第一个和第三个锚定标记

<td colspan="4" class="leftTopFormLabel">
    12. Details of Nominee :<span class="mandatoryField">*</span>
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('NomineeDetails.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes')"
        style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter Details Here</a>
</td>
</tr>
<tr class="lastData_Section" id="Tr12">
<td colspan="4" class="leftTopFormLabel">
    13. Family Particulars of Insured Person:
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('FamilyDetails_New.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes');"
        target="_blank" style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter
        Details Here</a>
</td>
</tr>
<tr class="lastData_Section" id="Tr18">
<td colspan="4" class="leftTopFormLabel">
    14. Details of Bank Accounts of Insured Person:<span class="mandatoryField">*</span>
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('EmployeeBankDetails.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes');"
        style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter Details Here</a>
</td>
</tr>

12获提名人详情:*
在此处输入详细信息
13被保险人的家庭资料:
进入
这里的细节
14被保险人的银行账户详情:*
在此处输入详细信息

有人能给我提个建议吗?

这应该行得通。使用contains方法分析onclick属性:

//a[contains(@onclick,'NomineeDetails.aspx')]
您的

13被保险人的家庭资料:
进入
这里的细节
14被保险人的银行账户详情:*
在此处输入详细信息
'''
soup=BeautifulSoup(数据,“html.parser”)
matches=soup.select(“a[onclick]:not([target]))
对于匹配项中的
打印(a.attrs.keys(),a.text)
from bs4 import BeautifulSoup

data = '''\
<td colspan="4" class="leftTopFormLabel">
    12. Details of Nominee :<span class="mandatoryField">*</span>
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('NomineeDetails.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes')"
        style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter Details Here</a>
</td>
</tr>
<tr class="lastData_Section" id="Tr12">
<td colspan="4" class="leftTopFormLabel">
    13. Family Particulars of Insured Person:
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('FamilyDetails_New.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes');"
        target="_blank" style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter
        Details Here</a>
</td>
</tr>
<tr class="lastData_Section" id="Tr18">
<td colspan="4" class="leftTopFormLabel">
    14. Details of Bank Accounts of Insured Person:<span class="mandatoryField">*</span>
</td>
<td colspan="2" class="lastFormValue">
    <a onclick="openWin('EmployeeBankDetails.aspx','','dialogWidth:1000px; dialogHeight:800px; center:yes');"
        style="text-decoration: Dynamic; color: Blue; cursor: pointer">Enter Details Here</a>
</td>
</tr>
'''

soup = BeautifulSoup(data, "html.parser")

matches = soup.select("a[onclick]:not([target])")
for a in matches:
    print(a.attrs.keys(), a.text)