我正在尝试阅读与Google Play商店中存在的应用相关的评论。我正在为此目的使用硒。每个评论都出现在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'))
但是,此语句返回“ None”。谁能指导我如何阅读与“ aria-label”相关的信息?
答案 0 :(得分:1)
您不能使用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
就像定位器一样,因为它们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()]
答案 1 :(得分:0)
您正在尝试读取标记的父项<div>
的属性,但该属性不存在。您需要按以下步骤修复代码:
print(a.find_element_by_xpah('.//div[@jscontroller and @jsmodel and @jsdata]//span[@class='nt2C1d']//div[@aria-label]').get_attribute('aria-label'))
答案 2 :(得分:0)
要阅读所有评论者的评分,您需要为visibility_of_all_elements_located()
引入 WebDriverWait ,并且可以使用以下Locator Strategies:
使用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