Python Selenium:访问 aria-label 信息

Python Selenium: accessing aria-label information

我正在尝试阅读 google Play 商店中与应用程序相关的评论。为此,我正在使用 Selenium。 jscontroller 中存在的每条评论 ="H6e0Ge"。

在 jscontroller = "H6e0Ge" 标签中,我正在尝试检索用户给出的评级与 "aria-label" 相关联,如图所示。

要读取所有评论者的评分,我的代码是

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'))

第三个打印语句读取评级,但问题是它对所有用户都保持不变。原因是因为我复制了第一个用户评级的完整 xpath,因此它对其他用户显示相同的评级。所以我用下面的语句替换第三个语句:

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

不过,这个说法returns"None"。谁能指导我如何阅读"aria-label"相关信息?

您正在尝试读取标签的父 <div> 的属性,但它不存在。您需要按如下方式修复您的代码:

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

您不能使用 H6e0Gehtml/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 喜欢定位器,因为它们 dynamically changes 并且不会很快工作。

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()]

要阅读所有评论者的评分,您需要为 visibility_of_all_elements_located() 引入 WebDriverWait,您可以使用以下 :

  • 使用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']")))])
    
  • 控制台输出:

    ['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']
    
  • 注意:您必须添加以下导入:

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