【问题标题】:How to extract the text 5000 from the table using Selenium and Java如何使用 Selenium 和 Java 从表中提取文本 5000
【发布时间】:2019-08-28 13:26:30
【问题描述】:

需要获取表中以下元素的XPATH: 这里的值 5000 是变化的,但单位电压是常量文本,需要获得具有以下元素概念的 xpath,在 xpath 下尝试过,但它不起作用:

//div[contains(text(),'')]/follows/div/span[contains(text(),'Unit voltage (V)')]

问题是每个环境都有 ID 更改,因此无法使用,需要帮助以获取上述格式的工作 xpath。

PFB HTML 代码部分

<tr class="v-formlayout-row v-formlayout-firstrow">
  <td class="v-formlayout-captioncell">
    <div class="v-caption v-caption-tiny v-caption-smalllabel v-caption-hasdescription">
      <span id="gwt-uid-207" for="gwt-uid-208">Unit voltage (V)</span>
    </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" id="gwt-uid-208" aria-labelledby="gwt-uid-207" style="">5000&nbsp;&nbsp;
    </div>
  </td>
</tr> 

【问题讨论】:

  • 你想要 xpath for 5000 或 xpath for label Unit voltage (V) 吗?
  • 我假设您需要 xpath 来提取不同的值 5000,在我的以下答案中建议使用 xpath。

标签: java selenium xpath webdriverwait xpath-1.0


【解决方案1】:

您可以使用以下给定的 xpath 来获取值 5000。

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

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

【讨论】:

    【解决方案2】:

    试试这个 -

    //span[contains(text(),'Unit voltage')]//ancestor::td//following::td[contains(@class,'contentcell')]//div
    

    或者

    //*[text()='Unit voltage']//following::td[contains(@class,'contentcell')]//div
    

    两者都应该工作。

    这些将返回您的 div 元素,使用 getText() 方法提取文本。

    【讨论】:

      【解决方案3】:

      要提取文本5000,您需要为visibilityOfElementLocated() 诱导WebDriverWait,您可以使用以下任一解决方案:

      • xpath 1

        System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Unit voltage (V)']//following::td[2]/div"))).getText());
        
      • xpath 2

        System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Unit voltage (V)')]//following::td[2]/div"))).getText());
        

      【讨论】:

        【解决方案4】:

        感谢大家的宝贵意见,xpath 下面对我有用,因为有多个元素,并且元素顺序在使用的环境中是相同的,如下所示:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-01
          • 2021-12-29
          • 2020-01-25
          • 2020-11-05
          • 1970-01-01
          • 1970-01-01
          • 2015-12-12
          • 2022-12-02
          相关资源
          最近更新 更多