2015-09-25 42 views
0

我知道你可以使用XPath,CSS或ID提取图像。但是,有些图像没有唯一的ID(或任何其他属性)。 HTML看起来像这样:Selenium:通过索引提取图像

<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

定位器稍后将用于识别图像..问题的主要目的是 - 是否有一种方法可以将上述值用作唯一标识符..? –

+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()); 

} 
0
driver.findElement(By.xpath("//span[@id='a-autoid-10-announce'][index_no]")) 

你可以通过改变index_no