Search code examples
pythonseleniumgetattribute

Extracting value of an element using Selenium


I'm using Python 3 and I need help with extracting the value of an element in a HTML code. The relevant part of the webpage code looks like this:

<span class="ng-isolate-scope" star-rating="4.61" size="22">

I'm currently using Selenium and the get_attribute function, but I have not been able to extract the 4.61 value. Since I have to loop over several webpages, the relevant part of my code looks like this:

stars=[]
i=driver.find_elements_by_xpath("//*[@star-rating]")
for y in i:
    temp=str(y.get_attribute("value"))
    stars.append(temp)

but it is not working as I would expect. Could you help me in terms of what I'm doing wrong here? Thanks a lot for your time!


Solution

  • Get the star-rating attribute instead of a value:

    temp = y.get_attribute("star-rating"))
    

    Note that you don't have to call str() on the result of get_attribute() - you'll get the attribute value as a string.

    You can also improve the code and collect the ratings in a single line using a list comprehension:

    stars = [elm.get_attribute("star-rating") 
             for elm in driver.find_elements_by_xpath("//*[@star-rating]")]
    

    And, if you need the ratings as floats, call float():

    stars = [float(elm.get_attribute("star-rating")) 
             for elm in driver.find_elements_by_xpath("//*[@star-rating]")]
    

    And, it would be a little bit more concise with a CSS selector:

    stars = [float(elm.get_attribute("star-rating")) 
             for elm in driver.find_elements_by_css_selector("[star-rating]")]