簡體   English   中英

如何使用 Xpath 為 selenium 選擇錨標簽?

[英]How to select anchor tag using Xpath for selenium?

我正在使用 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>

任何人都可以建議我嗎?

這應該有效。 使用 contains 方法解析 onclick 屬性:

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

您的<a>...</a>元素都沒有href屬性。 它們都沒有class屬性。

這是一種選擇具有onclick屬性但沒有target屬性的方法。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM