我知道您可以使用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开始向上移动。是否有可能以某种方式使用此值来唯一标识每个元素?
P.S
我知道我可以使用findElements并提取列表并遍历列表,但我想知道这是否也能正常工作。我是Java和Selenium。我正在查看的产品是:http://www.amazon.com/dp/B00I8BIBCW。
谢谢!
答案 0 :(得分:0)
您可以在列表中获取具有相同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());
}
答案 1 :(得分:0)
driver.findElement(By.xpath("//span[@id='a-autoid-10-announce'][index_no]"))
您可以通过更改index_no
来访问每个元素