What is the best method for retrieving text from the aria-label attribute?

Currently, I am delving into the world of web scraping. My goal is to extract the work-life balance rating from the Indeed website. However, the main obstacle I'm encountering is the extraction of text from the aria-label attribute so that I can retrieve the output of 4.0 out of 5 stars.

<div role="img" aria-label="4.0 out of 5 stars."><div class="css-eub7j6 eu4oa1w0"><div data-testid="filledStar" style="width:42.68px" class="css-i84nrz eu4oa1w0"></div></div></div>

Answer №1

To retrieve the value, it is essential to identify the specific element and then utilize the get attribute aria-label.

If you are working with Python, the code should look like this:

print(diver.find_element(By.XPATH, "//div[@role='img']").get_attribute("aria-label"))

Update:

print(diver.find_element(By.XPATH, "//div[@role='img' and @aria-label]").get_attribute("aria-label"))

Alternatively,

print(diver.find_element(By.XPATH, "//div[@role='img' and @aria-label][.//div[@data-testid='filledStar']]").get_attribute("aria-label"))

Answer №2

If you need to retrieve the value of a specific element attribute using Selenium, you can utilize the get_attribute() method.
For example, if you are working with By.ID and the locator is element_id.
The Python code would look like this:

attribute_value = driver.find_element(By.ID, element_id).get_attribute("attribute_name")

A similar approach can be taken in other languages with some minor syntax modifications.

Answer №3

To access the aria-label attribute value, which is "4.0 out of 5 stars," you can utilize a WebDriverWait method to ensure the visibility_of_element_located() and employ one of these locator strategies:

  • With CSS_SELECTOR and role="img":

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[role='img'][aria-label]"))).get_attribute("aria-label"))
    
  • Using XPATH and

    data-testid="filledStar"
    :

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-testid='filledStar']//ancestor::div[@role='img' and @aria-label]"))).get_attribute("aria-label"))
    
  • Note: Ensure you include these imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

For more insights, refer to this discussion: Python Selenium - get href value

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

Location-based services: Updating the position of a Google Maps marker without refreshing the entire map interface

How can I update only the marker on a map when the device is in motion or has increased accuracy? I want to reload the map when the position changes, but only move the marker. Below is the code snippet that I currently have: if (navigator.geolocation) { ...

The CSS child selector seems to be malfunctioning as it is affecting all descendants of the specified type

Struggling with creating a menu containing nested lists. Attempted using a child selector (#menu-novo li:hover > ul) to display only immediate descendants, but all are still showing. Any suggestions or solutions for this issue? #menu-novo-container { ...

Steps to insert a logo into a Bootstrap navbar

Hey there, I'm having trouble fitting my logo into a Bootstrap navbar. The issue is that the logo appears larger than the navbar itself, and I've tried various solutions without success. Can someone please assist me with this problem? Below is th ...

Issue encountered while attempting to retrieve the console logs in Chrome

Here is the code that I am working with: using FirstTestCase.PageObjectMethods; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Threading; namespace FirstTestCase { [TestFixture] class TestClassLog ...

Is it possible for the JavaScript DOM API to retrieve the exact casing of attribute and tag names?

Is it possible to retrieve the original casing of an attribute name or tag name from the source? The attribute name is in lowercase The tag name is in uppercase The local element name is in lowercase I am looking for a solution that doesn't require ...

Using the not operator in Selenium IDE XPath for waitForElementNotPresent seems to be ineffective

I am facing an issue with an XPath in Selenium IDE. In a table, there are multiple records. Above the table headers, there are filtering dropdown menus and a funnel icon that triggers an AJAX function to retrieve filtered data. The problem is that Seleniu ...

White border appears when hovering over MUI TextField

I've been troubleshooting this issue for what seems like an eternity. I've combed through Chrome's inspect tool, searching for any hover styles on the fieldset element, but to no avail. Here's my dilemma... I simply want a basic outline ...

Appium experiencing issues with driver.hidekeyboard() function causing errors

I implemented the code below: driver.findElement(By.id("FirstName")).sendKeys("Manoj"); driver.findElement(By.id("LastName")).sendKeys("Kumar"); driver.hideKeyboard(); According to this code, it successfully entered the first and last name. However, when ...

Guide on darkening the surrounding div of an alert to give it a modal-like effect

I want to display an alert to the user in a visually appealing way. To achieve this, I am utilizing Bootstrap's alert class. Here is how I am showing the user a div: <div class="alert alert-warning alert-dismissible" role="alert"> Some text ...

How can one transfer information from a client to a server and complete a form using JavaScript?

Can the information be transferred from a client to the server using just JS, then fill out a form on the server, and finally redirect the client to view a pre-filled form? I am considering utilizing AJAX to send a JSON object, but unsure if it will be s ...

An element will not automatically wrap text when all anchors are in place

http://jsfiddle.net/awWmY/ I've been struggling to figure out why the text won't wrap to #bigEnough. Can anyone help me with this simple issue? html{background:black;} #bigEnough { width: 500px; text-wrap: normal; } #bigEnough a { -moz-t ...

The HTML slideshow is not automatically showing up as intended

I need to make a few adjustments to the picture slideshow on my website. Currently, all the pictures are displayed at once when you first visit the site, and it only turns into a slideshow when you click the scroll arrows. I want it to start as a slideshow ...

Selenium for Ruby: Creating a unique custom scroller by scrolling down

I am trying to retrieve data that is displayed after I scroll a custom scrollbar within a website, not just the typical scrolling function. It seems like Selenium is unable to locate this information without performing the scrolling action first. I have ...

The process of converting a data:image base64 to a blob is demonstrated in this code snippet

Seeking a way to convert data:image base64 URLs to blob URLs. Below is the original code that generates the base64 URLs: <script> $(window).load(function(){ function readURL() { var $input = $(this); var $newinput = $(this ...

Padding will not completely fill the <li> and <div> elements

Looking to create a sleek menu bar that's 40px tall and fills up 80% of the browser width. The challenge arises when trying to center the text within the menu items. Despite adjusting padding for alignment, there's still a small gap because of & ...

Customizing error messages to display validation errors instead of default text errors in the Django UserCreationForm

I'm encountering an issue with the registration form on my website. The form itself is straightforward, but I'm facing a problem where if a username is already taken or if the two password fields don't match, Django doesn't respond afte ...

Ensuring React's setupProxy functions properly in conjunction with express.js

I've encountered an issue while trying to pass images from express to react. My express.static is correctly set up, so when I visit localhost:5000/public/images/me.jpeg, the image loads in my browser. However, when I attempt to use <img src="/publi ...

What steps do I need to take in order to programmatically click on the mp3 download button using my python code

Is there a way to automate downloading mp3 versions of YouTube videos from this site using Selenium? I've been working on some code but can't seem to get past clicking the download button. Can anyone provide assistance? from selenium import webdr ...

Struggling to Find Element in Selenium WebDriver Across Three Frames

I'm just starting out with Selenium Webdriver and I've encountered a couple of issues while trying to automate a webpage. One problem is that I am unable to click the Search button in the frame. Here's the snippet of code I have been using: ...

Dynamically checking conditions in Python using Selenium

I'm currently working on automation using selenium, but I am facing challenges in locating a specific element. I suspect the issue might be due to dynamic elements that sometimes display messages like "You found 0" or "You found 1." Below is the HTML ...