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