user2293224
user2293224

Reputation: 2220

Python Selenium-webdriver: for loop returning first element of the list throughout the loop

I am trying to retrieve users reviews related information from google play store. I am interested in gathering reviewer name, reviewer text, reviewer rating and review date. I am using selenium webdriver for retrieving infromation. Here is my code:

baseurl='https://play.google.com/store/apps/details?id=com.zihua.android.mytracks&hl=en&showAllReviews=true'
driver.get(baseurl)

person_info = driver.find_elements_by_xpath("//div[@class='d15Mdf bAhLNe']")

for person in person_info:
    name = person.find_element_by_xpath("//span[@class='X43Kjb']").text
    review = person.find_element_by_xpath("//div[@class='UD7Dzf']/span").text
    print(name)
    print(review)

The problem with my code is it returns the first name of the reviewers and its review throughout the loop. Could anyone guide me where am I making the mistake?

Upvotes: 2

Views: 696

Answers (1)

keepAlive
keepAlive

Reputation: 6655

It looks like you keep getting the same information again and again because each xpath-search starts from the beginning of the DOM. Something you can do is start your children-related xpaths with a '.' (dot) so as to only deal with children elements, as follows

for person in person_info:
    name = person.find_element_by_xpath(".//span[@class='X43Kjb']").text
    review = person.find_element_by_xpath(".//div[@class='UD7Dzf']/span").text
    print(name)
    print(review)

Upvotes: 4

Related Questions