How do I use an xpath in Java Selenium to interact with and click on a specific attribute

Recently, I came across an HTML snippet on a website that caught my attention. It involved accessing and clicking on a specific attribute value using Selenium and Java.

<a class="" data-images="{&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/156246/ma/VKFiZkcDgXY.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/156246/zo/VKFiZkcDgXY.jpg&quot;}" data-style-name="White" data-style-id="21049" data-sold-out="false" data-description="null" href="/shop/jackets/kiu5tqj83/myh25duje?alt=0" data-no-tubolink="data-no-tubolink">
    <img width="32" height="32" src="//assets.supremenewyork.com/156246/sw/VKFiZkcDgXY.jpg" alt="Vkfizkcdgxy">
</a>

In my quest to access the attribute's value with selenium & java and then click it, I attempted the following:

WebElement white = driver.findElement(By.xpath("//class[@data-style-name='White']"));
white.click();

However, this resulted in errors like:

"Unable to locate element: {"method":"xpath","selector":"//*[@id=details]/ul/li[3]/a[2]"} Command duration or timeout: 0 milliseconds"

Answer №1

Your XML path is not accurate. It must be

//a[@data-style-name='White']

or you can use one of these CSS selectors

a[data-style-name='White']
a[alt='Vkfizkcdgxy']

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

Applying style changes to the height on scroll event in Javascript for Chrome, Firefox, and Internet Explorer

I am working on triggering an event when scrolling to a specific height. I have code that successfully adds a style in Chrome, but it is not functioning properly in IE. Can anyone provide assistance? myID = document.getElementById("subnav"); var mySc ...

Ways to verify the content within two separate p elements

<p> expanded state:</p> <P> true </p> Merging the text from both tags into a single output. Final Output: expanded state: true ...

What could be causing my jQuery script to malfunction?

After scouring through numerous Stack Overflow questions and conducting countless Google searches, I am still stumped by the issue at hand. As a beginner in web development, all I want is for this simple page to function properly. Can someone please point ...

Enhance the vertical size of IcoFont

This code snippet includes CSS for styling a navbar, as well as HTML and Angular directives for creating navigation elements. If you're looking to center the <li> tags within the <nav> elements and increase the size of their IcoFont icons ...

transform an array of bits into a sequence of binary digits

Take a closer look at the lower section I have come across methods to convert hex values to binary before, but my current question is: can you convert hex values within a byte array into binary and concatenate them into one large string? The code I' ...

Extract specific fields from JSON in a single operation

My JSON file is large, but I only need to extract specific fields. I have the paths to these fields and have been using JPath successfully, but I want to streamline the process to parse all desired fields at once. For example, consider the following JSON s ...

Top Android framework for capturing a route within a mobile application

As a beginner in android app development, I am eager to create an app that can track a user's route and save the location details as a file for later display using the google maps api. I have searched for a java library that could help me achieve this ...

Swapping nodes in a custom tree structure using Java

I'm currently facing a challenge with a unique tree structure. My project involves creating a program that can "learn" about different animals. Let's dive into the nodes. I kick-started the process with a class known as BTNode, containing a strin ...

Adjusting the "width" of a widgets within a particular <td> element on WordPress

Recently, I was given the task of modifying a mortgage calculator widget on a WordPress site to enlarge its appearance. Specifically, I am attempting to adjust the #MLCalcFormLoanForm table tbody tr td where one of the individual tags is set with a width ...

Animate the sliding of divs using the JavaScript animation function

I've designed some boxes that function similar to notifications, but now I want to smoothly slide them in from the left instead of just fading in. I know that I need to use .animate rather than .fadeIn to achieve this effect. The code snippet I&apos ...

Experiencing difficulties with horizontal scrolling in Java/Selenium

After researching previous discussions about this issue, I am still struggling to make it work. I have a small scroll bar that I need to shift to the right in order to access the items I need for testing. Below is the code for the scroll bar: <div cl ...

Executing FindBugs in Java 5 mode: What is the recommended approach?

After using FindBugs on my Maven project, I keep encountering this error: Can't use annotations when running in JDK 1.4 mode! Does anyone know how to resolve this issue? I couldn't find a solution in the documentation. ...

The art of selecting elements and attaching event listeners in a React environment

Currently, I am working on my portfolio website using Gatsby. The layout includes a sidebar on the left with navigational links (Home, About, Work, etc.), and the main content is displayed as one long strip of sections on the right. When a user clicks on a ...

A different approach to achieving a newspaper-like text justification in HTML

When I have a paragraph with text-align:justify, the spacing between words can become uneven due to certain words. I used to rely on the property text-justify:newspaper; which helped break up the words for more evenly spaced text, but unfortunately, it is ...

Using Selenium to retrieve a compilation of all URLs within a closed issue page on GitHub

I am attempting to save the links of all the resolved issues from a GitHub project (https://github.com/mlpack/mlpack/issues?q=is%3Aissue+is%3Aclosed) using Selenium. The code snippet I am using is: repo_closed_url = [link.find_element(By.CLASS_NAME,'h ...

Creating a Personalized Dropdown Menu Within the <div> Tag

My current task involves obtaining an element in the following format: https://i.sstatic.net/uhHkL.png Inside the nav-item, there is a dropdown with a top-centered triangle on it, and the dropdown is positioned at the center. Below is what I have achiev ...

Creating a scrolling sidebar in Bootstrap 4

Today I learned about CSS Bootstrap 4 and created my template with a sidebar. However, when there are many menus, I want the sidebar to show a scrollbar. I am looking for help on how to make a scrollbar appear on my sidebar template. Below are my HTML and ...

Tips for implementing a wait function in Selenium WebDriver to ensure that dropdown values are populated before clicking a button

When I open a page in my application, a dropdown menu appears and I need to click on a proceed button. The problem arises when the dropdown takes some time to load its values and my code clicks on the button before the dropdown is fully loaded. I have at ...

Executing automated tests using Cucumber-Selenium within a QT browser that is embedded

I have a unique setup where I am running a GWT application within a C++ Desktop application. My task now is to trigger Selenium tests from the same browser that is embedded in this unique environment. The browsers at my disposal are IE and QT Browser. Can ...

Tips for defining boundaries for position absolute elements using JavaScript

Fiddle. I am in the process of developing a chat/mail application with a resizable menu similar to the Google Hangouts desktop app. When resizing the brown content pane at a normal speed, you can observe that the boundaries are functioning as intended. My ...