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

Utilizing the Selenium Chrome driver to interact with every active Chrome window

How can I use Selenium web driver in C# to locate and open a new tab with a specific URL in an existing Chrome window, rather than creating a new window within the ChromeDriver itself? IWebDriver driver = null; var chromeDriverService = Chrome ...

The lower division remains static when the browser window is resized

Why does the div with class .lower not move to the bottom of the page when the screen is resized? Could this be because of its CSS property position:absolute;? Check out the code snippet here. #lower { width: 100%; position: absolute; bot ...

Best practices for evenly distributing images within a navigation bar?

Within a horizontal navbar, I have various small images that are currently spaced out with different intervals between them. Each image has its own unique spacing from the others. I am looking to achieve consistent spacing between each image in this horiz ...

What is the best way to find a specific name among three items using xpath?

I have crafted an xpath to identify a name within a "th" tag, but the challenge is that there are three items enclosed within the "th" tag separated by a "br" tag. How can I specifically locate only the first item, which in this instance is the name among ...

iterating over a nested map within a map in an Angular application

I wrote a Java service that returns an observable map> and I'm currently struggling to iterate through the outer map using foreach loop. [...] .then( (response: Package) => { response.activityMap.forEach((key: string, value ...

New Method for Concealing Selenium Detection by Modifying navigator.webdriver Flag

Trying to automate a simple task on a website using selenium and Chrome has proven to be challenging as the website detects when Chrome is being controlled by selenium, blocking every request. It seems like the website may be utilizing an exposed DOM varia ...

Tips for verifying modifications on a webpage after refreshing it using Selenium and Python

I am facing a challenge where I need to continuously refresh a web page as quickly as possible and monitor for any changes. However, my initial approach did not yield the desired results. My attempt looked something like this: while True: driver.get( ...

I am looking for a way to add some color to the text within a div on my HTML page. Can you help me

Is there a way to apply background color only to the text content within this div, without styling the entire element? ...

When you zoom in or out, HTML5 elements styled with CSS will dynamically adjust

Hey everyone, I have a question about a problem I'm facing with my HTML and CSS page. The issue is that when I zoom in, the "Section" part moves underneath the nav bar, and when I zoom out, the footer moves next to the section part... Below is a sni ...

Error: A null node.ownerDocument is being encountered while attempting to use XPath in Selenium

I am facing an issue with crawling HTML content in Firefox using selenium. My goal is to extract the value of the attribute "title" (information needed) along with its text (additional information needed). The element I am interested in is inside an iframe ...

Strategies for managing a situation where one popup is activated by another popup

Trying out the latest version of Selenium (2.29) with Firefox 8.0.1 after encountering a modal dialog issue that limited me to using FF 11 as the maximum version. I have an icon that, when clicked, triggers some javascript code. Using the following snippet ...

Looking to interact with specific coordinates by clicking, holding, and moving using Capybara in Ruby

Currently, I am attempting to execute a click, drag, and release action at specific coordinates on a web page autonomously using Capybara. The objective is to interact with an element that contains a calendar which updates through mouse actions triggered b ...

CSS to target every second visible tr element using the :nth-child(2n)

My table has a unique appearance (shown below) thanks to the application of CSS nth-child(2n). tr:nth-child(2n) {background-color: #f0f3f5;} I made some elements hidden on the vID, ID, and MO_Sub tr. <tr style="display:none"> The table's new ...

Convert the easeInExpo function from jQuery easing to vanilla JavaScript and CSS

Currently, I am in the process of converting a piece of code from jQuery to plain JavaScript and CSS. The specific code snippet I am focusing on involves creating easing functions without relying on jQuery. const customEasing = { easeInExpo: function ( ...

Extracting text within quotation marks from HTML using Java and Selenium WebDriver

I encountered a piece of code that resembles the following: https://i.stack.imgur.com/gaX1J.png Although I am able to retrieve the link using the command System.out.print(link.getText()); it only returns the value "Saab". However, I also require the da ...

The head tag specification doesn't properly include the CSS file reference

I have a question regarding my HTML5 web page and the way I am referencing a CSS file in the head tag. Currently, I have it set up like this: <head> <link rel="stylesheet" href="css/style.css" type="text/css"/> </head> However, it d ...

What is the best method for enlarging the central image in Owl Carousel?

Could someone help me with implementing Owl Carousel to achieve this design? I have attempted it, but unfortunately, it is not working as expected. Your assistance in fixing the issue would be greatly appreciated. Please find the necessary code below: ...

The div element is not extending all the way to the left side of the browser window

I'm having trouble positioning a menu at the top of my webpage following a tutorial. The menu icon doesn't seem to reach all the way to the left side of the page. Despite multiple attempts to adjust the margin and padding settings, I can't ...

Alignment issue with text in Vuetify Datatables cells

I'm experimenting with Vuetify on my Laravel Vue application, and everything seems to be working fine except for the DataTables styles. The padding of the cells is not vertically center aligned as expected. Here's a screenshot Interestingly, the ...

Is the Rest Api failing to provide JSON string in the correct format?

When using REST to retrieve a URL resource that returns a string, the following code is used: URL url = new URL(some_url_string); HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); connection.setRequestMethod("GET"); BufferedReader ...