2015-09-25 43 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