I’m trying to get information that is within the anchor tag but not the href
. I want to extract the rating score from a few sellers on eBay. In the following HTML-Code, you can see where the rating score can be found. Is there a way to get the information about the "Bewertungspunktestand" (German for rating score) without using the href
, because the href
changes from the seller to seller? The rating score in this example would be 32. Since the text "Bewertungspunktestand" is only in this line, I thought it would be possible to let it search for this text and extract the aria-label with this text in it.
This is the link to this example.
This is the Python code I tried and didn’t work out:
try:
trans = driver.find_element_by_xpath("//a[@aria-label='Bewertungspunktestand']")
except:
trans = '0'
And this is the HTML-Code
<span class="mbg-l">
(<a href="http://feedback.ebay.de/ws/eBayISAPI.dll?ViewFeedback&userid=thuanhtran&iid=133585540546&ssPageName=VIP:feedback&ftab=FeedbackAsSeller&rt=nc&_trksid=p2047675.l2560" aria-label="Bewertungspunktestand: 32">32</a>
<span class="vi-mbgds3-bkImg vi-mbgds3-fb10-49" aria-label="Gelber Stern für 10 bis 49 Bewertungspunkte" role="img"></span>)
</span>
2
Answers
The value of aria-label attribute isn’t
Bewertungspunktestand
butBewertungspunktestand: 32
.To print the value i.e.
32
from theinnerHTML
you can use either of the following Locator Strategies:Using
css_selector
and text attribute:Using
xpath
andget_attribute()
:Ideally you need to induce WebDriverWait for the
visibility_of_element_located()
and you can use either of the following Locator Strategies:Using
CSS_SELECTOR
andget_attribute()
:Using
XPATH
and text attribute:Console Output:
Note : You have to add the following imports :
Outro
Link to useful documentation:
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.
Sure you can. Use XPATH’s contains method, combined with the abiltiy to select any attribute (@aria-label):
Specifically to get the text value of that link element: