有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSelenium:通过索引提取图像

我知道您可以使用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

谢谢


共 (2) 个答案

  1. # 1 楼答案

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

    您可以通过更改索引号来访问每个元素

  2. # 2 楼答案

    您可以在一个列表中获取具有相同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());
    
     }