如何使用 Selenium 和 Java 从 table 中提取文本 5000
How to extract the text 5000 from the table using Selenium and Java
需要获取 table 中以下元素的 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
</div>
</td>
</tr>
试试这个 -
//span[contains(text(),'Unit voltage')]//ancestor::td//following::td[contains(@class,'contentcell')]//div
或者
//*[text()='Unit voltage']//following::td[contains(@class,'contentcell')]//div
两者都应该有效。
这些将return你div元素,使用getText()
方法提取文本。
您可以使用下面给定的 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')]
要提取文本 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());
感谢大家的宝贵意见,下面的 xpath 对我有用,因为有多个元素并且元素顺序在以下使用的环境中是相同的:
(//tr//child::td[contains(@class,'contentcell')]//child::div[contains(@id,'gwt-uid')])[7]
需要获取 table 中以下元素的 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
</div>
</td>
</tr>
试试这个 -
//span[contains(text(),'Unit voltage')]//ancestor::td//following::td[contains(@class,'contentcell')]//div
或者
//*[text()='Unit voltage']//following::td[contains(@class,'contentcell')]//div
两者都应该有效。
这些将return你div元素,使用getText()
方法提取文本。
您可以使用下面给定的 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')]
要提取文本 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());
感谢大家的宝贵意见,下面的 xpath 对我有用,因为有多个元素并且元素顺序在以下使用的环境中是相同的:
(//tr//child::td[contains(@class,'contentcell')]//child::div[contains(@id,'gwt-uid')])[7]