skip to Main Content

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&amp;userid=thuanhtran&amp;iid=133585540546&amp;ssPageName=VIP:feedback&amp;ftab=FeedbackAsSeller&amp;rt=nc&amp;_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


  1. The value of aria-label attribute isn’t Bewertungspunktestand but Bewertungspunktestand: 32.

    To print the value i.e. 32 from the innerHTML you can use either of the following Locator Strategies:

    • Using css_selector and text attribute:

      driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true')
      print(driver.find_element_by_css_selector("a[aria-label^='Bewertungspunktestand']").text)
      
    • Using xpath and get_attribute():

      driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true')     
      print(driver.find_element_by_xpath("//a[starts-with(@aria-label, 'Bewertungspunktestand')]").get_attribute("innerHTML"))
      

    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 and get_attribute():

      driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true')
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[aria-label^='Bewertungspunktestand']"))).get_attribute("innerHTML"))
      
    • Using XPATH and text attribute:

      driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true')
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@aria-label, 'Bewertungspunktestand')]"))).text)
      
    • Console Output:

      MyMercy User
      
    • Note : You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium – Python


    Outro

    Link to useful documentation:

    Login or Signup to reply.
  2. Sure you can. Use XPATH’s contains method, combined with the abiltiy to select any attribute (@aria-label):

    //a[contains(@aria-label, 'Bewertungspunktestand:')]
    

    Specifically to get the text value of that link element:

    trans = driver.find_element_by_xpath("//a[contains(@aria-label, 'Bewertungspunktestand:')]").text
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search