【问题标题】:How to select anchor tag using Xpath for selenium?如何使用 Xpath 为 selenium 选择锚标记?
【发布时间】:2021-02-11 22:47:18
【问题描述】:

我正在使用 Selenium 进行网络自动化,但无法选择具有 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>

谁能推荐我?

【问题讨论】:

    标签: python html selenium webautomation


    【解决方案1】:

    这应该可行。使用 contains 方法解析 onclick 属性:

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

    【讨论】:

      【解决方案2】:

      您的所有&lt;a&gt;...&lt;/a&gt; 元素都没有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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-11
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 2011-03-09
        • 2020-09-06
        相关资源
        最近更新 更多