gpt4 book ai didi

java - Selenium:通过索引提取图像

转载 作者:行者123 更新时间:2023-11-28 07:09:07 27 4
gpt4 key购买 nike

我知道您可以使用 XPath、CSS 或按 ID 提取图像。但是,有些图像没有唯一 ID(或任何其他属性)。 HTML 看起来像这样:

<div id="altImages" class="a-fixed-left-grid-col a-col-left" style="width:40px;margin-left:-40px;_margin-left:-20px;float:left;">
<div id="thumbs-image" class="a-hidden" customfunctionname="(function(id, state, onloadFunction){ P.when('A').execute(function(A){ A.trigger('image-block-twister-swatch-click', id, state, onloadFunction); }); });"/>
<ul class="a-nostyle a-button-list a-vertical a-spacing-top-micro">
<li class="a-spacing-small template">
<span class="a-list-item">
<span class="a-declarative" data-thumb-action="{}" data-action="thumb-action">
<span id="a-autoid-10" class="a-button a-button-thumbnail a-button-toggle">
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="a-autoid-10-announce"/>
<span id="a-autoid-10-announce" class="a-button-text" aria-hidden="true">
<span class="placeHolder"/></span>
</span>
</span>
</span>
</span>
</li>
<li class="a-spacing-small item">
<span class="a-list-item">
<span class="a-declarative" data-thumb-action="{"index":"0", "thumbnailIndex":"0", "type": "image", "variant": "MAIN"}" data-action="thumb-action">
<span id="a-autoid-10" class="a-button a-button-thumbnail a-button-toggle">
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="a-autoid-10-announce"/>
<span id="a-autoid-10-announce" class="a-button-text" aria-hidden="true"></span>
</span>
</span>
</span>
</li>
<li class="a-spacing-small item">
<span class="a-list-item">
<span class="a-declarative" data-thumb-action="{"index":"1", "thumbnailIndex":"1", "type": "image", "variant": "PT01"}" data-action="thumb-action">
<span id="a-autoid-10" class="a-button a-button-thumbnail a-button-toggle">
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="a-autoid-10-announce"/>
<span id="a-autoid-10-announce" class="a-button-text" aria-hidden="true"></span>
</span>
</span>
</span>
</li>
<li class="a-spacing-small item">
<span class="a-list-item">
<span class="a-declarative" data-thumb-action="{"index":"2", "thumbnailIndex":"2", "type": "image", "variant": "PT02"}" data-action="thumb-action">
<span id="a-autoid-10" class="a-button a-button-thumbnail a-button-toggle a-button-selected a-button-focus">
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="a-autoid-10-announce"/>
<span id="a-autoid-10-announce" class="a-button-text" aria-hidden="true"></span>
</span>
</span>
</span>
</li>

所有元素的 ID 都是 id="a-autoid-10-announce"。元素之间的唯一区别是这部分:data-thumb-action="{"index":"0", "thumbnailIndex":"0" - 其中值从 0 开始向上移动. 是否有可能以某种方式使用此值来唯一标识每个元素?

附言

我知道我可以使用 findElements 并提取一个列表并遍历该列表,但我想知道这是否也有效。我是 Java 和 Selenium 。我正在查看的产品是:http://www.amazon.com/dp/B00I8BIBCW .

谢谢!

最佳答案

您可以在列表中获取具有相同 ID 的所有元素,然后遍历列表以针对单个元素采取操作。如下所示:

List<WebElement> imgList = driver.findElements(By.id("a-autoid-10-announce"));
for(WebElement img : imgList)

{
//do something with image.
System.out.println(img.getText());

}

关于java - Selenium:通过索引提取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com