Java and Selenium: Struggling to Find Element based on Attribute

Attempting to click a button on a webpage with the given HTML code:

<html lang="en" webdriver="true">
<head>
<body class="scbody" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAKCAYAAAB10jRKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBJREFUeNpEyqERADAIBMGbbwSH+qhA/6UR... // The shared code

<p>The crucial portion of the code is this:</p>

<pre><code> <a class="action actionLink" aria-describedby="141494853656_title" aria-label="Remove" href="http://cart.payments.ebay.com/sc/rfc?iref=8460053414" data-irefid="8460053414">Remove</a>
    <span class="mr10 ml10 linkClr">|</span>
    <a class="action actionLink" aria-describedby="141494853656_title" aria-label="Save for later" href="http://cart.payments.ebay.com/sc/sfl?iref=8460053414" data-irefid="8460053414">Save for later</a>

I considered using the aria-describeby attribute to dynamically store an item for later like so:

".//*[@aria-describedby=" + "'" + itemID + "_title']/div[2]/div/div/a[2]"

However, it's not functioning as expected. Where am I going wrong?

Keep in mind that there could be multiple items and identifying a specific one solely relies on the itemID since other attributes are generated dynamically.

Thank you

Answer №1

Personally, I believe that relying too heavily on xpath can lead to inefficient coding practices. Instead, consider utilizing cssSelector for better results:

driver.findElement(By.cssSelector(".action")).click();

If you need to locate an element by its attribute, a more effective approach would be something along these lines:

By.CssSelector("button[name='hasan']");

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

`The logo does not dim when the popup is displayed`

I'm currently experiencing an issue with pop-ups on my page - when they appear, they create a shade over all elements of the page except for my two logos and are displayed with a border around them. Here's what my page looks like before the popu ...

What causes the content area of my <input> element to extend beyond the boundaries of its parent <span> element?

Please Note: I've been working on extracting code from a WordPress environment running the Salient theme. As of now, I haven't been able to simplify the issue to its core form. There is a possibility that I might figure it out myself, but I welco ...

Prevent Second Division from wrapping around floated Division

I am running into a minor issue with the positioning of a div in my current project. +------------------------+ |+-------+ ~~~~ TITLE| <--how can I prevent text from wrapping around || | ~~~~~~~~~~~~~ |\ on the left ...

Establish a connection using Angular 4 HTTP POST to communicate with a Java REST API, as it is not permitted on the server

I am facing an issue with my Angular 4 client service that is calling a REST method declared on a Java server using a PostMapping annotation. When I make the call from Angular, it is not accepted by the server. However, when testing it on Postman, it work ...

Contact on the go with our handy mobile contact form

Having some difficulties with the contact form on my website. The link to the form is: here. I am currently working on making the form mobile-friendly (the rest of the site scales down using @mobile screen tags in the CSS). The form is stacked, so there ...

testing cannot be performed when a data provider is referenced

Upon adding "dataProvider" and "dataProviderClass" to my APpium test, I encountered the following error: [Configuration] [WARN] Detected a static method [genericLib.BaseClass.InitiateDriver()]. Static configuration methods can cause unexpected behavior. [C ...

Issue encountered during conversion of Java project to Maven project

Recently, I decided to try out Eclipse for my Selenium automation learning journey. Everything was going smoothly until I added the Java environment variable. However, once I tried converting the Java project to a Maven project, I encountered some errors t ...

Can you provide the date time format used in the JSTL fmt tag?

To display date and time in JSTL, the fmt tag can be utilized. Details can be found here In order to format the date for use with front end tools like the datatable, a specific date format needs to be specified. By using parameters such as type or dateSty ...

What is the Java version of a GET request in Python?

I'm struggling to achieve a similar task in Java, specifically when it comes to setting cookies. While I know how to accomplish this in python: response = requests.get(app_url,cookies=cookies,allow_redirects=False) I'm unsure of the equivalent ...

iOS hover menu behavior: Close dropdown when the same element is tapped again on iPads

The navigation bar features categories such as: politics, economy, education Upon clicking on these categories, a dropdown menu appears with locations like: asia, europe, North America What I am looking for is this: If the user clicks (tabs) or hovers (o ...

"Which is better for maximizing the efficiency of an image grid: CSS or Jquery? What are the key

As a UX Designer looking to enhance my coding skills, I must admit my code may not be perfect. Please bear with me as I navigate through this process. I am in the process of revamping my portfolio website. The original seamless grid was created using a Ma ...

Is it possible for me to include &x&y&z in my Sass code?

When working with Sass, I have the following code: asdf{ &-b&-c {font-size: 16px;} } And I want the generated CSS to look like this: asdf.asdf-b.asdf-c{ font-size: 16px; } Is it possible to achieve this in Sass without having to repeat th ...

Set the background image width to 75% of the screen size

I want to use this image as a background for a section on my page. My preference is to have the white part cover only 75% of the screen, stopping before reaching the content in the left grid. Is it possible to achieve this? I attempted the following CSS: ...

I encountered a Null Pointer Exception while trying to retrieve information from the Excel spreadsheet

Code: public class ContactFormTest { WebDriver driver; @BeforeMethod public void setUp() { System.setProperty("webdriver.chrome.driver", "Driver Path"); driver=new ChromeDriver(); driver.get("URL"); driver.manage().deleteAllCookies(); driver.manage().wind ...

The xpath locator is failing to accurately pinpoint an "<a" element with a complex class name

I'm having issues with this line of code, even though I believe my xpath is correct. driver.findElement(By.xpath("//a[contains(@class,'cke_button_bold')]")).click(); The goal is to locate a button like the one below : <a id="cke_73" c ...

What is the process for creating a stationary container positioned on the left side, which extends vertically down the page to showcase a collection of skills?

Hey there, I'm a newcomer to web development. For my very first project, I'm working on a resume page. I'm looking to have a fixed left div where I can showcase my skills, and a larger div on the right side for the main body content containi ...

Applying media queries to incorrect screen sizes

My media query seems to be behaving oddly. @media only screen and (max-width: 1200px) { .tool-panel { margin: 0px 24px 0px 0px; } } It is getting applied at 1183.20px instead of 1200px. This discrepancy is consistent across all browsers ev ...

Spring is unable to link MultiValueMap to a bean

When sending x-www-form-urlencode data to Spring, the following code works fine: @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public void sumbitFUD(@RequestBody final MultiValueMap<String, Str ...

Cannot see the toggler button in Bootstrap 4 Navbar

I recently implemented the toggler button (hamburger icon) code in my HTML, but I'm facing an issue where the hamburger icon doesn't appear and looks blank when I resize my screen or view it on a smaller viewport. Here's the snippet of my H ...

"Reduce the height of the navigation bar when the user scrolls down the webpage

I am having an issue with my fixed navigation bar created using bootstrap 4. I want to reduce the height of the navbar as I scroll down the page, but it does not seem to be working. Here is a link to a video showing the problem in action: Link to the vide ...