简体   繁体   中英

Python Selenium: accessing aria-label information

I am trying to read the reviews related to app present on google play store. I am using Selenium for this purpose. Each review present in jscontroller ="H6e0Ge".

在此处输入图片说明

Inside jscontroller = "H6e0Ge" tag, I am trying to retrieve the rating given by the user is associated by the "aria-label", as shown in the picture.

在此处输入图片说明

To read rating of all reviewers, my code is

driver = webdriver.Chrome('/Users/yasirmuhammad/Downloads/chromedriver')
driver.get('https://play.google.com/store/apps/details?id=com.axis.drawingdesk.v3&hl=en&showAllReviews=true')
for a in driver.find_elements_by_xpath("//*[@class='d15Mdf bAhLNe']"):
    print(a.find_element_by_class_name('X43Kjb').text)
    print(a.find_element_by_class_name('p2TkOb').text)
    print(a.find_element_by_xpath('/html/body/div[1]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/div/span[1]/div/div').get_attribute('aria-label'))

Third print statement reads the rating, but the problem is it remain same for all users. The reason is because I copied the full xpath of rating of first user, hence it shows the same rating for other users. So I replace the third statement with below statement:

print(a.find_element_by_class_name('pf5lIe').get_attribute('aria-label'))

However, this statement returns "None". Could anyone guide me how should I read the "aria-label" related information?

You cannot use H6e0Ge and html/body/div[1]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/div/span[1]/div/div like locators, because they dynamically changes and will not work very soon.

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

reviews = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//h3[.='User reviews']/following-sibling::div[1]/div")))
for review in reviews:
    print(review.find_element_by_xpath(".//span[1]").text)
    print(review.find_element_by_xpath(".//span[2]").text)
    print(review.find_element_by_xpath(".//div[@role='img']").get_attribute('aria-label'))
    print(review.find_element_by_xpath("descendant::div[@jscontroller][last()])").text)

Xpaths:

//h3[.='User reviews']/following-sibling::div[1]/div//span[1]
//h3[.='User reviews']/following-sibling::div[1]/div//span[2]
//h3[.='User reviews']/following-sibling::div[1]//div[@role='img']
//h3[.='User reviews']/following-sibling::div[1]/div/descendant::div[@jscontroller][last()]

You are trying to read the attribute of the parent <div> of the tag, but it is not there. You need to fix your code as follows:

print(a.find_element_by_xpah('.//div[@jscontroller and @jsmodel and @jsdata]//span[@class='nt2C1d']//div[@aria-label]').get_attribute('aria-label'))

To read rating of all reviewers you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategies :

  • Using XPATH :

     driver.get('https://play.google.com/store/apps/details?id=com.axis.drawingdesk.v3&hl=en&showAllReviews=true') print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//h3[text()='User reviews']//following::div[1]//span[text()]//following::div[1]//div[@role='img']")))])
  • Console Output:

     ['Rated 4 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 1 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 4 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 4 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 4 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars', 'Rated 5 stars out of five stars']
  • 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

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