Help Please on 'driver.find_elements_by_xpath'

103 views
Skip to first unread message

Kieran Dooley

unread,
Nov 2, 2018, 10:38:13 AM11/2/18
to Selenium Users
Hi

I am brand new to Python - please and thanks in advance for your patience.

I have a site of customer reviews where I want to pull out the number of stars out of 5 that a reviewer gave a provider 

In the below example, the reviewer has given the provider 1 star out of 5.  I want to be able to pull the "1" from the <meta itemprop="ratingValue" content="1"> below

<div class="rvw__hdr">
  <div class="rvw__hdr-stat" itemtype="http://schema.org/Rating" itemscope="" itemprop="reviewRating">
    <meta itemprop="worstRating" content="1">
<img data-rating="1.0" src="//media.consumeraffairs.com/static/img/icons/stars/stars-1.a9f9f49815b7.svg" alt="Rated with 1 star" class="stars-rtg stars-rtg--sm">
<meta itemprop="ratingValue" content="1">
<meta itemprop="bestRating" content="5">
  </div>
</div>

I have spent hours on this.
The nearest I have is the following:
 
rating = driver.find_elements_by_xpath('//div[@class="rvw__hdr-stat"]')

num_page_items = len(rating)
for i in range(num_page_items):
    print (rating[i].text)

This returns emptiness
when I omit the.text, I get objects returned for each of the 30 reviews on the page, one of which is as follows:

<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="271fc297-c03f-42b2-a680-2b5dd5656227", element="e1066956-81f4-4203-a2ae-5d3cdd95789a")>

I am clueless as to how to turn this into what I want, the actual rating number provided by the reviewer

I have tried other variations of driver.find_elements_by_.... with no luck



Thanks in advance for any help

Kieran Dooley

unread,
Nov 2, 2018, 3:23:01 PM11/2/18
to Selenium Users
I have managed to resolve this in 2 ways - Incase anyone is looking for similar 

I first succeeded by using BeautifulSoup instead of Selenium


Then on Selenium (with a little help from a friend). 

To get overall rating, you can get it from the attribute of div tag


overall_rating = driver.find_element_by_css_selector("div.stars-rtg")
print(overall_rating.get_attribute("data-rating"))


To get all consumer review ratings on first page, I can get it from the attribute of the img tag.


ratings = driver.find_elements_by_css_selector("img.stars-rtg")
ratings_value = [rating.get_attribute("data-rating") for rating in ratings]
print(ratings_value)

 
Reply all
Reply to author
Forward
0 new messages