【问题标题】:How to extract the text using XPath from the following Child elements如何使用 XPath 从以下子元素中提取文本
【发布时间】:2023-03-14 08:50:02
【问题描述】:

我的数据行很少,它们没有可跟踪的 id 或类,因此需要一个 child/followed 类型的 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 变化,我们需要验证这两个元素。

为此,我需要一个 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

【问题讨论】:

  • 您是否要提取文本,例如CHDBF1080?

标签: java selenium xpath webdriverwait xpath-1.0


【解决方案1】:

你可以试试这个 xpath。这使用“单元类型”来定位子元素(它们是动态的)。此 xpath 将返回两个元素。所以你必须遍历它来获取文本。

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

【讨论】:

    【解决方案2】:

    提取文本,例如CHDBF1080相对于文本Unit type作为元素是动态元素,你要诱导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());
      

    【讨论】:

      【解决方案3】:

      使用下面的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"));
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-26
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多