如何使用 Xpath 计算 DIV 内的 IMG 标签
How to count IMG tags within DIV using Xpath
我想统计div标签下有多少个IMG标签并回显数量
<div id="surface" style="width: 4567px; height: 4137px; left: -1850px; top: -1152px; cursor: default;">
<img src="https://media.memories.png" data-seat="L:106|EE:5" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2221px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:6" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2237px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:7" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2253px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:8" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2269px; top: 1561px; display: block;">
</div>
这是我尝试获取计数的非工作尝试,但它没有将计数返回到 运行 我在 selenium IDE.
中的测试
"//div[@surface='data-seat']/img"
尝试:
//div[@surface='data-seat']/count(img)
对于您的示例 html,它输出 4
。
选项 1:
您可以简单地使用下面的 xpath 然后获取计数。
图像的 xpath - "//div[@id='surface']/img"
System.out.println(driver.findElements(By.xpath("//div[@id='surface']/img")).size());
选项 2:
如果你想让它更复杂,使用 js 运行 查询和 return 计数。
JavascriptExecutor js = (JavascriptExecutor) driver;
Long number_of_imgs = (Long) js.executeScript("return document.querySelectorAll(\"div[id='surface'] img\").length");
System.out.println(number_of_imgs);
我更喜欢选项 1。
您可以使用 XPath-1.0 表达式
count(//div[@id='surface']/img)
它计算所有 div
个元素的所有 img
个子元素。
要在 <div>
标签内打印 count 个 <img>
标签,您需要引入 WebDriverWait对于 visibilityOfAllElementsLocatedBy()
,您可以使用以下任一项 :
xpath
:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='surface']//img"))).size());
cssSelector
:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div#surface img"))).size());
我想统计div标签下有多少个IMG标签并回显数量
<div id="surface" style="width: 4567px; height: 4137px; left: -1850px; top: -1152px; cursor: default;">
<img src="https://media.memories.png" data-seat="L:106|EE:5" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2221px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:6" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2237px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:7" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2253px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:8" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2269px; top: 1561px; display: block;">
</div>
这是我尝试获取计数的非工作尝试,但它没有将计数返回到 运行 我在 selenium IDE.
中的测试"//div[@surface='data-seat']/img"
尝试:
//div[@surface='data-seat']/count(img)
对于您的示例 html,它输出 4
。
选项 1: 您可以简单地使用下面的 xpath 然后获取计数。
图像的 xpath - "//div[@id='surface']/img"
System.out.println(driver.findElements(By.xpath("//div[@id='surface']/img")).size());
选项 2: 如果你想让它更复杂,使用 js 运行 查询和 return 计数。
JavascriptExecutor js = (JavascriptExecutor) driver;
Long number_of_imgs = (Long) js.executeScript("return document.querySelectorAll(\"div[id='surface'] img\").length");
System.out.println(number_of_imgs);
我更喜欢选项 1。
您可以使用 XPath-1.0 表达式
count(//div[@id='surface']/img)
它计算所有 div
个元素的所有 img
个子元素。
要在 <div>
标签内打印 count 个 <img>
标签,您需要引入 WebDriverWait对于 visibilityOfAllElementsLocatedBy()
,您可以使用以下任一项
xpath
:System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='surface']//img"))).size());
cssSelector
:System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div#surface img"))).size());