【问题标题】:XPath for an element with same class names具有相同类名的元素的 XPath
【发布时间】:2021-02-24 15:42:45
【问题描述】:

谁能帮我导出 xpath(从第二个 div 跨度元素标签,即 GP)

<div class="ohg-patient-banner-suppl-info-section ohg-patient-banner-suppl-info-custom pure-u-1-5">
            <div class="ohg-patient-banner-suppl-info-component-container ohg-patient-banner-suppl-info-component-left-bordered ohg-patient-banner-suppl-info-custom" aria-label="Visit Info" role="group"><div class="ohg-patient-banner-suppl-info-section-summary" aria-hidden="false">
    
        
    
</div>
<div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="true">
    
        <div class="ohg-patient-banner-suppl-info-custom-field">
            <span class=" " aria-hidden="true"></span>
            
                <span class=" ohp-metadata-label">Location</span>
            
            <span class="ohg-patient-banner-suppl-info-custom-row-value-icon  " aria-hidden="true"></span>
            <span class=" ohg-patient-banner-suppl-info-value">Tauranga Hospital - Assmt Plan Unit TAU - </span>
        </div>
    
        
</div>
</div>
</div>

<div class="ohg-patient-banner-suppl-info-section ohg-patient-banner-suppl-info-custom pure-u-1-5">
            <div class="ohg-patient-banner-suppl-info-component-container ohg-patient-banner-suppl-info-component-left-bordered ohg-patient-banner-suppl-info-custom" aria-label="GP Info" role="group"><div class="ohg-patient-banner-suppl-info-section-summary" aria-hidden="false">
    
        
</div>
<div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="true">
    
        <div class="ohg-patient-banner-suppl-info-custom-field">
            <span class=" " aria-hidden="true"></span>
            
                <span class=" ohp-metadata-label">GP</span>
            
            <span class="ohg-patient-banner-suppl-info-custom-row-value-icon  " aria-hidden="true"></span>
            <span class=" ohg-patient-banner-suppl-info-value">-</span>
        </div>

</div>
</div>
    
</div>

我编写的 XPath 适用于第一个 div,它返回值 Location:

.//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohp-metadata-label']

【问题讨论】:

    标签: c# selenium xpath


    【解决方案1】:

    Xpath:

    //*[@class='ohg-patient-banner-suppl-info-custom-field']//span[2]
    

    那么你可以使用 gettext() 来获取 GP 作为文本

    【讨论】:

      【解决方案2】:

      试试这个 XPath-1.0 表达式:

      //*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field' and span[@class=' ohg-patient-banner-suppl-info-value']='-']/span[@class=' ohp-metadata-label']
      

      它的结果是:

      GP

      【讨论】:

        【解决方案3】:
        1. 程序化解决方案:

        您的 xpath 实际上确实返回了两个元素:

        在您的 selenium 库中,您很可能收到一个数组,并且可以选择它的第二个元素

        1. 选择第二个元素:

        如果它总是将[2] 添加到 xpath 的第二个元素有帮助 例如"(.//*[@class='ohg-patient-banner-suppl-info-section-detail'])[2]/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohp-metadata-label']"

        1. 固定文本

        如果您在页面中有一些固定的文本,例如Location 您可以将其用作参考,然后使用 ancestor 和 sibling 轴

        ".//span[.='Location']//ancestor::div[@class='ohg-patient-banner-suppl-info-section-detail']//following-sibling::div[@class='ohg-patient-banner-suppl-info-section-detail']//span[@class=' ohp-metadata-label']"

        【讨论】:

        • 谢谢你的回复,我尝试了你提到的第一个 XPath,我得到了 NoSuchElementException ( Message=Unable to find element with xpath == .//*[@class='ohg-patient -banner-suppl-info-section-detail'][2]/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class='ohp-metadata-label' ])
        • 对不起,我的回答有误,现在应该更正
        【解决方案4】:

        由于您没有其他唯一标识符,并且类名由 2 个跨度使用,因此您可以根据其索引识别该跨度,并且是最短的方法:

        xpath:  (//span[@class=' ohp-metadata-label'])[2]
        

        现在您可以使用 selenium getText() 方法抓取文本。 请注意,我们使用索引 2 来标识您的定位器,但如果 html 代码会发生变化,并且在此之前会添加新的类似 span,您将需要更改索引。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-17
          • 2017-07-27
          • 1970-01-01
          • 1970-01-01
          • 2014-07-04
          • 2014-09-05
          相关资源
          最近更新 更多