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

时间:2019-09-26 12:08:09

标签: java selenium xpath webdriverwait xpath-1.0

我的数据行很少,它们没有可跟踪的ID或类,因此需要有一个child /按照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>

这里单位类型是需要作为父元素的元素,并且该值不会更改,但接下来的元素 CHDB F1080 更改,我们需要验证这两个元素。

要做到这一点,我需要一个XPath,该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

3 个答案:

答案 0 :(得分:0)

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

//span[text()='Unit type']/following::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("//span[text()='Unit type']/following::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"));
    }

答案 1 :(得分:0)

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

//div[contains(text(),'Unit type')]/parent::td/following-sibling::td//tbody//td[contains(@class,'v-label')]

答案 2 :(得分:0)

要提取文本,例如关于文本单元类型 CHDB F1080 ,因为这些元素是动态元素,因此必须引入 WebDriverWait visibilityOfElementLocated(),您可以使用以下任一Locator Strategies

  • xpath 表示文本 CHDB

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr//span[text()='Unit type']//following::td[@class='v-formlayout-contentcell']/div[starts-with(@id, 'gwt-uid-') and starts-with(@aria-labelledby, 'gwt-uid-')]/div[@class='v-slot v-slot-hide-indicator']//div[@class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w"][@style]"))).getText());
    
  • xpath 表示文本 F1080

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr//span[text()='Unit type']//following::td[@class='v-formlayout-contentcell']/div[starts-with(@id, 'gwt-uid-') and starts-with(@aria-labelledby, 'gwt-uid-')]/div[@class='v-slot v-slot-hide-indicator']//div[@class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w" and not(@style)]"))).getText());