简体   繁体   English

Python Selenium:访问 aria-label 信息

[英]Python Selenium: accessing aria-label information

I am trying to read the reviews related to app present on google play store.我正在尝试阅读与 google Play 商店中的应用程序相关的评论。 I am using Selenium for this purpose.为此,我正在使用 Selenium。 Each review present in jscontroller ="H6e0Ge". 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.在 jscontroller = "H6e0Ge" 标签中,我试图检索用户给出的评级与“aria-label”相关联,如图所示。

在此处输入图片说明

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.原因是因为我复制了第一个用户评分的完整 xpath,因此它对其他用户显示相同的评分。 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?谁能指导我如何阅读“aria-label”相关信息?

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.您不能使用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就像定位器一样,因为它们是动态变化的,不会很快起作用。

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: Xpath:

//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.您正在尝试读取标签的父<div>的属性,但它不存在。 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 :要阅读所有评论者的评分,您需要为visibility_of_all_elements_located()引入WebDriverWait ,您可以使用以下定位器策略

  • Using XPATH :使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用aria-label通过Python3和Selenium定位并单击元素 - Using aria-label to locate and click an element with Python3 and Selenium 如何通过带有 Python Selenium 的 aria-label 找到并单击 JavaScript 按钮? - How to find and click a JavaScript button by aria-label with Python Selenium? 如何使用 selenium 和 python 查找 aria-label - How to do find aria-label using selenium and python 我想通过 python selenium 从“aria-label”获取文本 - I want to get text from "aria-label" by python selenium 如何使用带有 aria-label 的 selenium 单击 python 中的按钮 - How to click on a button in python using selenium with aria-label 如何在 python 中抓取 aria-label 文本? - How to scrape aria-label text in python? python标签的Scrapy提取值 - Python Scrapy Extract Value of aria-label Python Selenium 单击按钮,没有名称、没有 id、没有文本、许多类和一个 aria-label - Python Selenium click button with no name, no id, no text, many classes, and an aria-label 使用 Selenium 的 find 函数检索 aria-label 值 - Retrieving aria-label value using Selenium's find function 如何根据使用Selenium的html从使用xpath找到的元素中检索属性aria-label的值 - How to retrieve the value of the attribute aria-label from element found using xpath as per the html using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM