Tips for accessing attribute values with Selenium and CSS selectors

Below is the HTML code snippet I am working with:

<a href="/search/?p=2&q=move&mt=1"> 2 </a>

I am trying to extract the content of the href attribute, specifically "/search/?p=2&q=move&mt=1". Could someone provide me with the appropriate command and CSS locator for this task using Selenium?

If I have multiple similar elements like:

<a href="/search/?p=2&q=move&mt=1"> 2 </a> 
<a href="/search/?p=2&q=move&mt=2"> 3 </a> 

How would I construct a CSS locator syntax to target the href attribute value containing '2' in the text?

Answer №1

If your HTML code only contains an <a> tag, you can achieve the desired result with the following line of code:

String href = selenium.getAttribute("css=a@href");

To accomplish this, utilize the DefaultSelenium#getAttribute() method. Simply supply a CSS locator and append an @ symbol followed by the attribute name you wish to retrieve. In our scenario, we are selecting the a element and retrieving its @href attribute.

In regard to your recent comment/edit:

  1. The section after @ indicates to Selenium that it represents the attribute's name.

  2. When constructing the locator, ensure you place :contains('2') before @href as it is part of the locator syntax rather than an attribute. The revised line should appear as follows:

    selenium.getAttribute("css=a:contains('2')@href");
    

Answer №2

Try changing css=a@href to just href. Let me know if this doesn't work.

    List<WebElement> elements = driver.findElements(By.className("c"));
        for(WebElement element : elements)
        {
           String doctorName = element.getText();
           String linkValue = element.getAttribute("href");
        }

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

Explore button that gradually decreases max-height

I have a "Show More" button that expands a div by removing the css attribute max-height, but I want to add an animation similar to jQuery's slideToggle() function to smoothly reveal the rest of the content. This is the code I am using: <div id="P ...

Only conceal the breadcrumb trail on the 404 error page

I have recently created a custom node for my site's 404 page with the path /node/83 in the site information. However, I am facing an issue where I cannot hide the .breadcrumb element using display:none. I attempted to achieve this through CSS injector ...

Executing Javascript within a Robot Framework test case can be achieved by using the Run Keyword

When attempting to run JavaScript and return a value using run keyword if or similar methods, I encounter an error as anticipated. How can I resolve this issue? Below is an example code snippet: Run Keyword If '${COUNTRY}'=='ES' ${inco ...

The focus is on the last row that was selected

I've noticed that when I check and select a row in ui-grid, only the last selected row is focused with a background color. Should all rows be automatically painted by default? Thank you. ...

Encountering difficulties when trying to input text using Send Keys in Selenium and executing script

I am struggling to create a login script for the website https://i.sstatic.net/nAThP.png Every time I attempt to input Keys into the email field, I encounter this error: selenium.common.exceptions.ElementNotInteractableException: Message: element not in ...

"Optimizing the placement of a range slider for pricing options

As a beginner in HTML, CSS and JS, I recently faced the challenge of creating a price slider range on a website. However, I am struggling with repositioning it. After copying the code from this link, I noticed that the slider is positioned at the top of th ...

Discover the elements that possess the ::after pseudo-element

I'm looking to extract specific labels from a list of labels. <div> <label class="search-reusables__value-label"> ::before <p class="display-flex">...</p> </label> <label class=" ...

What is the best way to assign an image to a div using JavaScript or jQuery?

I'm attempting to create a notification box similar to this one: https://i.sstatic.net/HIJPJ.png As shown, there is a .gif image positioned at the top that will be hidden once the content loads. Now, I want to insert the following image into a <d ...

Mobile CSS Problem with Wordpress Header Image

Currently working on a website and facing an issue with the header resizing incorrectly on mobile devices. The header shifts to the right instead of staying centered above the navigation like it does on desktop screens. Below is the code snippet being used ...

Utilizing SASS with React: An in-depth guide to integrating SASS into your React projects

When it comes to using SASS in React, the first step is... To get started, you need to install node-sass and then add import ./mysass.scss in your index.js file. I followed these steps successfully with Bootstrap's SASS integration. In fact, I mana ...

Having difficulties with building a HTML layout using Bootstrap functionality

I am struggling to recreate a specific view using Bootstrap. The image of the view I want to achieve can be seen here: https://i.sstatic.net/VtaMc.png My attempt at coding this view using Bootstrap is as follows: <div class="col-md-12"> <di ...

What are the best practices for utilizing headless Chrome in conjunction with Selenium?

I am curious about the best way to achieve this task in 2021, especially with the changes that have taken place. It seems like people are approaching it differently now. My goal is to automate my Twitter follow bot discreetly in the background using headl ...

Encountering a permission error when attempting to save the index.php file?

We encountered an issue when trying to save the index.php file on FileZilla. The error message displayed was: Error: /home/devratnacricket/public_html/admin/index.php: open for write: permission denied Error: File transfer failed What steps should we t ...

"Selecting multiple options is not possible as the size attribute cannot be utilized

My <select multiple="multiple">..</select> element is set to a height of 1-row due to the select{heigth: 30px;} style in an inaccessible stylesheet. The "size" attribute does not work in this case. What can I do to fix this issue? ...

Ways to toggle the expansion and collapse of multiple divs efficiently with jQuery without duplicating code

I am trying to create a script that will expand and collapse a div containing a paragraph when the header text above it is clicked. <div class="container"> <h3><span class="toggler">Click here to toggle text</span></h3> <d ...

Troubleshooting Problem with Accordion Size in CSS

I am facing an issue with a dropdown menu that I have created. The dropdown has parent and child rows to display controls, but the width of the Accordion is not stretching as expected despite being set to 100%. Using Chrome and Edge developer tools, I insp ...

Server log for Selenium Webdriver

Could someone please provide guidance on how to check the webdriver server log? In the past, we were able to view the log by running selenium server from the command line. I am currently using the latest standalone server with firefox driver. I attempted t ...

I'm having trouble applying CSS stylings to my components in my React project. What could be the issue causing this?

As a React beginner, I am facing issues with getting my CSS to work properly in my project. I know that CSS is not used directly in React, but rather interpreted as JavaScript using webpack and babel. What have I tried? I attempted to make changes to my w ...

Ways to create a downward expanding navigation menu when collapsed

Trying to accomplish the following, please refer to the image below. I am looking to have the yellow bar extend down when the collapsed menu is open. Currently, the navigation links are hidden behind the logo as shown in the second image below. https://i. ...

Is it possible to upload an image file while the element is hidden with "display: none" property?

I need to upload a file using Selenium webdriver. Here is the URL of the page. <div class="async-upload__thumb item-image__area"> <div class="fab-dialog__thumb-drop-zone async-upload__thumb-drop-zone"> <p class="async-upload__thumb-ms ...