如何使用XPath从以下子元素中提取文本

问题描述 投票:0回答:3

我的数据行很少,它们没有可跟踪的ID或类,因此需要XPath的子级/。以下是HTML内容:

<tr class="v-formlayout-row v-formlayout-firstrow" xpath="1">
  <td class="v-formlayout-captioncell">
    <div class="v-caption v-caption-smalllabel v-caption-hide-indicator v-caption-hasdescription"><span id="gwt-uid-6138" for="gwt-uid-6139">Unit type</span></div>
  </td>
  <td class="v-formlayout-errorcell">
    <div class="v-formlayout-error-indicator"></div>
  </td>
  <td class="v-formlayout-contentcell">
    <div class="v-horizontallayout v-layout v-horizontal v-widget smalllabel v-horizontallayout-smalllabel hide-indicator v-horizontallayout-hide-indicator" id="gwt-uid-6139" aria-labelledby="gwt-uid-6138">
      <div class="v-slot v-slot-hide-indicator">
        <div class="v-formlayout v-layout v-widget hide-indicator v-formlayout-hide-indicator">
          <table cellpadding="0" cellspacing="0" role="presentation">
            <colgroup>
              <col>
            </colgroup>
            <tbody>
              <tr class="v-formlayout-row v-formlayout-firstrow v-formlayout-lastrow">
                <td class="v-formlayout-captioncell">
                  <div class="v-caption v-caption-tiny v-caption-smalllabel"></div>
                </td>
                <td class="v-formlayout-errorcell">
                  <div class="v-formlayout-error-indicator"></div>
                </td>
                <td class="v-formlayout-contentcell">
                  <div class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w" style="">CHDB&nbsp;&nbsp;</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
      <div class="v-slot v-slot-hide-indicator">
        <div class="v-formlayout v-layout v-widget hide-indicator v-formlayout-hide-indicator">
          <table cellpadding="0" cellspacing="0" role="presentation">
            <colgroup>
              <col>
            </colgroup>
            <tbody>
              <tr class="v-formlayout-row v-formlayout-firstrow v-formlayout-lastrow">
                <td class="v-formlayout-captioncell">
                  <div class="v-caption v-caption-tiny v-caption-smalllabel"></div>
                </td>
                <td class="v-formlayout-errorcell">
                  <div class="v-formlayout-error-indicator"></div>
                </td>
                <td class="v-formlayout-contentcell">
                  <div class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w">F1080&nbsp;</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </td>
</tr>

这里Unit type是需要作为父元素的元素,并且该值不会更改,但是接下来的元素CHDBF1080会更改,我们需要验证这2个元素元件。

要做到这一点,我需要一个XPath,它需要将Unit类型作为父元素,并将我们作为孩子得到的值用于同一模式中的多个值,因此这将很有帮助。

目前正在使用

(//tr//child::td[contains(@class,'v-formlayout-contentcell')]//child::div[contains(@id,'gwt-uid')])[1]
(//tr//child::td[contains(@class,'v-formlayout-contentcell')]//child::div[contains(@id,'gwt-uid')])[2]

这不是一个好习惯,因此将第一个值作为父项,然后将其作为孩子或使用同级函数需要可重用的XPath

java selenium xpath webdriverwait xpath-1.0
3个回答
0
投票

使用下面的XPath。这将返回您所需要的两个元素。

//table[@role='presentation']//td//div[contains(@class,'v-label-undef-w')]

您需要诱发WebDriverWaitvisibilityOfAllElementsLocatedBy()并使用getAttribute()通过innterTexttextContent来获取值>

WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table[@role='presentation']//td//div[contains(@class,'v-label-undef-w')]")));
for(int i=0;i<elements.size();i++)
   {
      System.out.println(elements.get(i).getAttribute("innerText"));
      System.out.println(elements.get(i).getAttribute("textContent"));
    }

0
投票

您可以尝试使用此xpath。这使用“单元类型”来定位子元素(动态元素)。该xpath将返回两个元素。因此您必须遍历它才能获取文本。


0
投票

要提取文本,例如对于文本Unit type

© www.soinside.com 2019 - 2024. All rights reserved.