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

Issue with React: Caret icon in dropdown menu shifts position when page is resized to iPad or smaller dimensions

I'm facing an issue with a styled react component that has a custom dropdown menu featuring a caret icon. Whenever I test the responsiveness of the dropdown, the caret icon ends up outside the intended area. To resolve this, I've wrapped the drop ...

When it comes to creating a CSS drop-down menu, don't forget to incorporate an additional <ul

I've created a drop-down menu using CSS with 5 visible list items. When hovering over one of the list items, it highlights and triggers a drop-down effect to display another list underneath. Essentially, you have an initial set of options, and upon ho ...

Tips for customizing text field appearance in Safari and Chrome

I haven't had a chance to test it on IE yet. My goal is to create a unique style for the background image and text box shape, along with borders, for the search bar on my website, [dead site]. If you check it out on Firefox or Opera and see the sear ...

Display an icon to act as a separator between icons in CSS styling

Is there a way to display a spacer line before and after icons (cross symbols) while excluding the spacer line before and after buttons carrying the word "Cancel"? How can this be achieved? This is my CSS file: .Container > *:first-child::before, .Con ...

An issue occurred while executing PhantomJS using Selenium's RemoteWebDriver

Launching a selenium grid hub using the default startup command: java -jar selenium-server-standalone-2.33.0.jar -role hub Also, initiating PhantomJS in its webdriver mode on the same machine with the following command: phantomjs --webdriver=8080 --web ...

additional one hour of generated report added >

My Select option code is causing some issues. When I view the uploaded data, there seems to be a duplication and an extra tag at the end: <option value="Running Request |Running > 1 hour">Running Request |Running > 1 hour</option> The ...

Font Awesome icons may not display in HTML

Hey there, I'm struggling to get checkbox icons and a plus symbol to appear on my checkbox tags on my website. I added this CSS code to the main .css file of the page, but unfortunately the icons are not displaying. Here is the CSS snippet that includ ...

Angular fails to function properly post rendering the body using ajax

I recently created a HTML page using AJAX, and here is the code snippet: <div class="contents" id="subContents" ng-app=""> @RenderBody() @RenderSection("scripts", required: false) </div> <script src="http://ajax.googleapis.com/ajax/ ...

Is it necessary to always include all styles for jQuery UI separately in my project?

While reading various articles, I have noticed a common suggestion to explicitly bundle each jQueryUI .css file, such as the answer provided on How to add jQueryUI library in MVC 5 project?. However, upon examining the .css files within the Content/themes/ ...

Tips for utilizing com.thoughtworks.selenium.DefaultSelenium in Eclipse for script writing

When I was scripting in Eclipse using Selenium RC, I encountered an error with com.thoughtworks.selenium.DefaultSelenium. It seems that the com.thoughtworks.selenium class is missing from my library. Does anyone have a suggestion for how to fix this issu ...

Creating a sleek and functional dashboard using HTML and leveraging the power of Bootstrap 4

I'm working on styling my HTML dashboard with Bootstrap 4. I want the text to be big and have minimal white space, but not too cluttered. <html> <head> <meta charset="utf-8"> <meta name="viewport" conte ...

Having Trouble with Selenium Webdriver on Firefox?

I am diving into the world of automating tasks with Selenium Webdriver for the first time. I have installed Eclipse and downloaded the latest selenium standalone server jar and webdriver jar. Here is a glimpse of my project progress so far: https://i.ssta ...

HTML's Nesting Buttons for Enhanced Interactivity

Can anyone help me figure out why the inner buttons in my HTML code are displaying outside of the parent button? I am attempting to showcase buttons inside a large button on my webpage. Your feedback on what I might be doing wrong here would be greatly a ...

Troubleshooting problems with iframes in Selenium

I'm currently working on automating the process of entering a consumer number, selecting a region, navigating to the next page, and downloading a PDF file. These are the specific steps I am focusing on streamlining. My main challenge lies in download ...

Altering webpage content through the use of Ajax

I need a solution for dynamically updating web page content using JavaScript AJAX. One idea I had was to store different div layouts in separate files, like so: BasicDiv.div: <div> <p>Some Text</p> <button> A Button </ ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

Ways to display multiple PHP pages in a single division

Within my project, I have a unique setup involving three distinct PHP pages. The first file contains two divisions - one for hyperlinked URLs and the other for displaying the output of the clicked URL. Here is an excerpt from the code snippet: <script& ...

Finding an element that lacks both a class and an id, and is not consistently present - what's the trick?

Currently, I am faced with a predicament in my code where a <li> element only appears under specific conditions, making it difficult to apply positioning. This element lacks an id and class attribute, which prevents me from targeting it accurately us ...

Tips for obtaining a string in Java without ' ' or ' ' characters when using the gettext function

Here is my code snippet: <a href="/trade/view/92204"> 2008 Honda Odyssey - 5dr Wgn EX-L </a> After using gettext, the returned string is: "\n\t\t\t\t\t\t\t200 ...

ChromeDriverService and Azure Pipelines Agents

Currently, I am automating Selenium tests in an Azure DevOps pipeline using C#. To improve debugging, I recently updated my code to include ChromeDriver logs, requiring me to make changes to utilize the ChromeDriverService. string binaryDir = Manag ...