Extracting Text from a Span Element Using XPath in Selenium

Here is the HTML code snippet:

<div class="a-row a-spacing-small a-size-small">
<div class="a-row">
<a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":"false","url":"/gp/customer-reviews/widgets/average-customer-review/popover/ref=wl_it_o_cm_cr_acr_img_hz?ie=UTF8&a=B05555JQP&contextId=wishi&link=1&seeall=1","name":"review-hist-pop.B075555RJQP","max-width":"700","position":"triggerBottom","data":{"itemId":"I2555555554GT","isGridViewInnerPopover":""},"header":"","cache":"true"}">
<i id="review_stars_I2J55555554GT" class="a-icon a-icon-star a-star-4-5">
<span class="a-icon-alt">4.5 out of 5 stars</span>
</i>
<i class="a-icon a-icon-popover"/>
</a>
<a class="a-link-normal g-visible-no-js" href="/product-reviews/B075555JQP/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=2K4U5555551D&coliid=I2J5555555T&showViewpoints=1">
<span class="a-letter-space"/>
<a id="review_count_I2J55555555GT" class="a-link-normal" href="/product-reviews/B05555555P/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=255555555D&coliid=I2555555GT&showViewpoints=1">(68)</a>
</div>
<div class="a-row">
<div class="a-row a-size-small itemAvailability">
<div class="a-row itemUsedAndNew">
</div>

I am attempting to retrieve the text 4.5 out of 5 stars using either of these XPath expressions:

.//*[contains(@id,'review_stars')]/span[@class='a-icon-alt']
.//*[contains(@id,'review_stars')]

However, my attempts have been unsuccessful so far and are resulting in an empty string.

Interestingly, all these XPaths work perfectly fine in Firebug, so I am unsure why they are not working in my program. My suspicion is that this issue may be related to the fact that the rating is only visible when hovering over a specific element on the webpage. I am uncertain about how this might be causing the problem or how to resolve it.

Any insights would be appreciated! Thank you!

Answer №1

Your mistake was not properly placing the image between the anchor and span elements. The span should be inside the image, not next to the anchor as a sibling.

Consider using this code:

.//*[contains(@id,'review_stars')]/i/span[@class='a-icon-alt']

Answer №2

Although I am struggling to grasp the reason why my previous code is not functioning as expected, I am determined to find a solution. If someone can offer me a comprehensive explanation, I will consider their response as the definitive answer.

At this moment, the workaround that seems to be effective for me involves substituting element.getText(); with

element.getAttribute("innerHTML");

While this method yields the desired outcome, I am keen on understanding the underlying reasons why getText() fails in this particular scenario. Once again, if anyone can furnish me with a functional XPath or provide clarification on this matter, I am prepared to acknowledge it as the final resolution.

Many thanks.

Answer №3

To obtain the rating of 4.5 out of 5 stars using XPath, you should utilize the following code:

//a[@class='a-link-normal a-declarative g-visible-js reviewStarsPopoverLink']/i[starts-with(@id,'review_stars_') and @class='a-icon a-icon-star a-star-4-5']/span[@class='a-icon-alt']

Update :

You mentioned that

This does not work either. I just tried it.
. It seems like you might have omitted a section in the xpath that was provided by me. The method I recommended has been proven to be effective. Refer to the screenshot below for confirmation:

https://i.sstatic.net/gfRXI.png

Note: Although your inquiry pertained to xpath, you opted to present your answer focusing on getText() and getAttribute("innerHTML") methods. Nevertheless, my solution is compatible with both getText() and getAttribute("innerHTML") methods.

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

Tips for choosing a drop-down menu option without an identifier using Selenium in Python

I am trying to choose an element/item from a drop-down menu in Python and Selenium without using an id element. Here is the HTML code snippet: <mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope"> <select class="form-con ...

Transform black and white image to vibrant colors and add various hover effects

There are three images displayed on this webpage: This website is built on WordPress and utilizes the WP Bakery plugin for designing layouts. The images here are set to change from color to grayscale, and back to color upon mouseover. The following CSS c ...

I require the flexbox children to be arranged in a regular manner

How can we set flexbox children to default layout behaviors? Let's make them behave like the disobedient kids they are. .wrapper { This should be a row } .column { Make all heights equal display: flex; } .child { Back to regular display...wit ...

Newbie Query: How can I apply various font weights to a single font within the same stylesheet?

Twice I have inserted the font Acumin Pro Condensed, once with a weight of 400 and again with a weight of 700. I attempted to use both weights separately; assigning the 400 weight to an h3 tag and the 700 weight to an h2 tag. However, only the 700 font we ...

Is the java.lang.IllegalStateException exception being thrown in the main thread?

I've attempted to download the updated chrome driver and included it in my drivers folder, but I'm still encountering the same error. import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome ...

Automating NW.js Technology Application with Selenium WebDriver Script

I am currently working on automating my standalone application built using NW.JS technology, which incorporates the chromium embedded framework. NW.JS is a unique desktop application that utilizes web technologies. The application I am testing is embedde ...

Issue: Unable to deserialize Entities.Student object from START_ARRAY token in com.fasterxml.jackson.databind.JsonMappingException

I am facing an issue with my 'JudoClass' object that holds an arrayList of 'Student' objects. Every time I attempt to create a new student, the error mentioned above pops up. Below is the post method code snippet: @POST @Produces(Medi ...

Having Trouble Choosing Options From Dropdown Menu Using Index in Selenium with Java

I am familiar with how to choose an element from a dropdown using the selectByIndex method. However, when attempting to use selectByIndex (select.selectByIndex(index)) on the HTML snippet below: <select id="destinationAllocationId" name="destinationAll ...

What is the best method for extracting a specific set of elements from MongoDB?

Looking to extract specific elements from a Mongodb Table. Let's consider having two variables in the Employee class: public Class Employee { private String Id; private String Name; . . When executing the fetch query, it will look something like th ...

Scrape the data within a specific div class and store it as a JSON object or in a data frame

I am trying to automate the process of clicking on each ID in a table on a specific webpage and extracting the information to a python object, such as a JSON or dataframe. Currently, I am able to locate the table with the desired data, but I am struggling ...

Tips for Controlling an Absent Search Bar in the HTML Source Code with Selenium

I'm attempting to automate searches using Selenium in Java. Upon inspecting the search bar on the page, I noticed that it has an id of searchBox and a name of q, which could be helpful. However, these properties are not visible in the HTML when viewi ...

Tips for rendering JSON text on a Java Canvas

Is there a way to display a nicely formatted JSON object/string on an Android Canvas? Here's an example: val jsonText = "{"profile":{"name": "Robert","account_id": "31"}}" This JSON string will be rendered on a Canvas like this: https://i.sstatic. ...

As the window width decreases, the image is breaking free from its container

When the window is in full screen, such as lg or md, the content fits within the container. However, when I resize the window to be smaller, the image overflows outside of the container. This layout was created using Bootstrap 3. Below is the HTML and CSS ...

Creating a sticky footer using Vue.js for both mobile and desktop display views

Looking for a solution to create a sticky footer that works on both desktop and mobile views without overlapping the main content? The current code either overlaps or doesn't stick to the bottom. Seeking a responsive approach. App.vue: <template& ...

Extract the image URL from the href attribute using Xpath

My goal is to extract all images enclosed in href attributes from the given code snippet <div class="jcarousel product-imagethumb-alt" data-jcarousel="true"> <ul> <li> <a href="https://domain/imagefull.jpg" onclick="return false;" cla ...

How can I trigger a JavaScript event using Selenium?

I am currently utilizing the syntax for selenium WebDriver. I understand that in selenium server-based syntax, you have the ability to trigger a javascript event by executing: Selenium selenium = new DefaultSelenium("localhost", server.getPort(), ...

Achieving Vertical Alignment of Text and Icon in the Same Line Using HTML and CSS

I am struggling to align these icons vertically center with the text. However, there seems to be a slight offset of a few pixels. I have experimented with different solutions, but this is the closest I could get to. Can anyone explain why it's not ali ...

Using Python for implementing explicit waits in Selenium

This specific question addresses issues related to Python and not the Java problem mentioned in another discussion thread. Currently, I am exploring Selenium's documentation on explicit waits, but I am having trouble creating code examples for each u ...

Is the row border camouflaged by the background?

feed_page: { margin: 'auto' }, feed_list: { margin: 'auto' }, feed_item: { textAlign: 'center', backgroundColor: '#fff', borderBottom: '1px solid #e0e0e0', margin: '10 ...

How can I make a div element match the height of an image and overflow without relying on

Here is the code that I am currently working with: https://codepen.io/allen-houng/pen/ExYKYdq Additionally, you can find a gist of the code snippet below: <div class="parent"> <div class="imageWrapper"> <img class="image" src="imageurl" ...