Getting images using index in Selenium

When it comes to extracting images from HTML, we usually rely on XPath, CSS selectors, or unique IDs. However, there are cases where images lack any distinguishable attributes. The structure of the HTML is as follows:

<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>

All elements share the ID id="a-autoid-10-announce". The distinguishing factor among them lies in the snippet

data-thumb-action="{"index":"0", "thumbnailIndex":"0"
, where the values increment sequentially starting from 0. Is it feasible to leverage this value for unique identification?

P.S.

In addition to using findElements and iterating through a list, could the described method serve as an alternative? I'm working with Java and Selenium. The target product can be found at: http://www.amazon.com/dp/B00I8BIBCW.

Thank you!

Answer №1

To gather all elements with the same id in a list, you can use the following method to iterate through each element and take action accordingly:

List<WebElement> linkList = driver.findElements(By.id("a-autoid-10-announce"));
for(WebElement link : linkList)

  {
     //perform actions on each link
     System.out.println(link.getText());

 } 

Answer №2

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

To target specific elements, you just need to adjust the index_num

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble importing JUnit and Truth libraries into Android Studio?

Following the instructions in the official documentation, I created a class for unit tests and tried to import com.google.common.truth.Truth.assertThat and org.junit.Test libraries. However, Android Studio failed to recognize these imports and displayed er ...

How can I delete a video on mobile in WordPress?

Looking for some assistance with my webshop www.spidercatcher.dk. I'm trying to have a background video display only on PCs, while showing a still photo on phones and tablets. I've tried using a poster tag but I can't seem to scale it proper ...

Tips for moving a physical mouse with two frames on a webpage

When logging into the project, there is a page with two frames. I encountered an issue where the mouse would not move from one frame to the other when using the Robot class for automation. I also attempted to use Selenium code to switch between frames. He ...

Error: The Android application encountered a JSONException due to a missing value

I have searched extensively for solutions to a specific error in Android development, but none of them seem to address my problem. I am new to Android development and struggling to display data in a listView for an app. Below is the code snippet causing t ...

The preflight response is denying the request header field ack, despite the server permitting it

This question has been circulating on various platforms, but this one is a bit unique. I encountered an error while attempting to access a Java API using axios with the following code: axios .get("http://127.0.0.1:8090/api/v1/homescreen") .the ...

Gatsby utilized the background-image CSS style within the Emotion library for CSS-in-JS implementation

The background image is not visible with the current setup. To troubleshoot, I attempted to set the background to pink within const background and it worked. This confirms that Emotion is functioning correctly. Upon inspecting with the React Dev Tools ext ...

Preventing the ScrollTop Function from Triggering in Jquery Upon Link Click

Hey there! I'm currently facing an issue with jquery scrollTop. Whenever I navigate to the id #linkA and click it again, an unnecessary scroll is added. I'd like to prevent scrolling after clicking the link. Imagine these three paragraphs have a ...

Utilizing Selenium Grid in Conjunction with Page Object Model Page Factory

I am looking to optimize my browser initialization process for testing on multiple browsers using the Page Object Model Page Factory. Currently, I have the following setup in my Base Class: // initialize driver/browser public void initDriver() throws Ma ...

Which selector in CSS is applicable for targeting Bootstrap tooltips?

After some experimentation, I've mastered the functionality of Twitter Bootstrap Tooltips and am now focused on customizing their appearance. Previous inquiries into other plugins revealed specific CSS selectors - for example, jScrollPane had a track ...

How to Retrieve MySQL Binary(16) UUID Using Java

Feeling a bit lost here with this seemingly simple question, just one of those days... Unfortunately, unable to use Hibernate or any other ORM. Working with Java PreparedStatement. MySQL details: CREATE TABLE `article` ( `articleID` binary(16) NOT NUL ...

Issue with displaying modal and backdrop specifically on iPhone in Angular 13

Within our Angular 13 application, we have a modal component. The component's CSS includes the :host selector for the root element, which also serves as the style for the backdrop: :host { position: absolute; background: rgba(0, 0, 0, 0.5); widt ...

The phenomena of endless looping between Hibernate and Thymeleaf

I am facing an issue with two classes that have a OneToMany/ManyToOne relation mapped with one attribute. The problem arises when I try to select and pass the object to the view, as I want to parse it to JavaScript using Thymeleaf. However, this causes an ...

E/AndroidRuntime: CRITICAL ERROR: main -Running on Android 5.1

My project for a calculator app keeps crashing on my device and I am getting the error E/AndroidRuntime: FATAL EXCEPTION: main. I am testing it on a Huawei Y3II running Android 5.1. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tes ...

Is ghostdriver required to utilize selenium and phantomjs in java?

I have experience using Selenium 2 and PhantomJS with Python in the past, and there is plenty of support available for that combination. However, I am now interested in trying this setup using Java. Has anyone else attempted this? Is GhostDriver necessar ...

Setting up AWS Elastic Beanstalk environment for Python applications with Selenium: A step-by-step guide

I am encountering an issue with my AWS Elastic Beanstalk Python 2.7 environment where my application utilizes selenium. Specifically, I have the line of code: from selenium.webdriver.common.desired_capabilities import DesiredCapabilities Oddly enough, wh ...

Adjusting the transparency of the background without impacting the text itself

I've been struggling to create a loading screen with no luck. The animation itself looks fine, but the background is causing issues. Every time I try to adjust the opacity, it affects the text as well. @import url('https://fonts.g ...

Text not aligning properly when utilizing the <p> tag

I'm struggling to align my text in a row, as shown below: https://i.sstatic.net/SGQ8I.png "Investup" and "Real estate" should be aligned. I need to move the "Real estate" paragraph so that it aligns with the "invest up" paragraph. Here's my co ...

How can I extract multiple texts from a single column using Selenium?

As a beginner with Selenium, I am trying to locate specific positions on a webpage. How can I retrieve all genres from the top 200 games (4 pages) at once? Check out the website link: here Sample My initial goal is to extract the top 50 games from the f ...

How to Perfectly Position Content with Bootstrap 3's Full Width Image Carousel

Here's my query: Is there a way to adjust the position of my content (such as text) inside the slider responsively? The text can be positioned anywhere within the Div Slider, for example, at 25% from the left and 50% from the top within the Image Di ...

What is the best way to position two buttons side by side on a single row

Below is the code snippet: <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix modal-footer"> <div class="ui-dialog-buttonset"> <button type="button" class="btn btn-mini btn-info"> ...