简体   繁体   中英

How to find aria -label element using contains in xpath

I'm trying to get an information which 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 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 of this example: 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

This is the python-code i tried and didn't worked 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>

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

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

To print the value ie 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:

From your query what I understand is that you want to get all the aria-label in the page. Below XPath will return all the the aria-label values on the webpage which you can traverse through using loop.

//span[@class='mbg-l']/a/@aria-label

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM