【问题标题】:Selenium: Extract Images via IndexSelenium:通过索引提取图像
【发布时间】:2015-09-25 07:45:10
【问题描述】:

我知道您可以使用 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="a-autoid-10-announce" - 页面上有 3 个..
  • 我不会帮你违反亚马逊的服务条款。你不应该抓取他们的网站。

标签: java css selenium xpath


【解决方案1】:

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

 } 

【讨论】:

    【解决方案2】:
    driver.findElement(By.xpath("//span[@id='a-autoid-10-announce'][index_no]")) 
    

    您可以通过更改 index_no 来访问每个元素

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2016-02-14
      • 2018-12-06
      相关资源
      最近更新 更多