Extracting value of an element using Selenium
<p>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:</p>

<pre><code>&lt;span class="ng-isolate-scope" star-rating="4.61" size="22"&gt;
</code></pre>

<p>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:</p>

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

<p>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! </p>

<p>Get the <code>star-rating</code> attribute instead of a <code>value</code>:</p>

<pre><code>temp = y.get_attribute("star-rating"))
</code></pre>

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

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

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

<p>And, if you need the ratings as floats, call <code>float()</code>:</p>

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

<hr>

<p>And, it would be a little bit more concise with a <a href="http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_elements_by_css_selector" rel="nofollow">CSS selector</a>:</p>

<pre><code>stars = [float(elm.get_attribute("star-rating")) 
         for elm in driver.find_elements_by_css_selector("[star-rating]")]
</code></pre>

python selenium getattribute