0
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class FindByXpathCss():

    Reviews = []
    reviewText = []
    driver = webdriver.Chrome(executable_path=r"F:\Chrome-webdriver\chromedriver.exe")
    driver.maximize_window()
    baseUrl = "https://play.google.com/store/apps/details? 
    id=com.delta.mobile.android&hl=en_US&showAllReviews=true"
    driver.get(baseUrl)

    driver.find_element_by_tag_name('body').send_keys(Keys.ARROW_DOWN)
    time.sleep(3)

    review_btn = driver.find_elements_by_xpath("//button[contains(@class,'')][contains(text(),'Full 
    Review')]")

    for btn in review_btn:
        btn.click()

I am able to get only one review even though I have used List type to get all the reviews, please help to print all the reviews. Moreover, its not getting all reviews (only getting till some reviews)

    for textreview in reviewText:
        Reviews.append(textreview.text)
        print(Reviews)

        driver.close()
4
  • how are you getting value in reviewtext, add that code , – PDHide Mar 31 '20 at 9:20
  • use getAttribute('textContent') instead of getText might fix the issue if you are sure about the logic – PDHide Mar 31 '20 at 9:21
  • reviewText = driver.find_elements_by_xpath("//div[2]/span[1]") , This is the code I am using to get reviewText of each reviews. – Sivakumar Prakash Mar 31 '20 at 12:35
  • Still one more issue is there, div tag having 2 span tags, if the review is short one span tag having text, if it crossed some limit, another span having it,.. for i in range(1, 40): if driver.find_element_by_xpath("//div[@jscontroller='LVJlx'][1]//button")[i].is_enabled(): reviewFullText = driver.find_elements_by_xpath("//div[@jscontroller='LVJlx']//span[2]") else: reviewFullText = driver.find_elements_by_xpath("//div[@jscontroller='LVJlx']//span[1]") for reviewText in reviewFullText: Reviews.append(reviewText.text) – Sivakumar Prakash Apr 8 '20 at 17:55
1

Below is the code for doing that in java , you can use the same logic in python.

The logic is:

  1. Get all displayed review, store the length to a variable 'tmp'
  2. Scroll to last review
  3. Check whether show more is displayed, if displayed then click it
  4. Get all reviews again, check whether the current length == 'tmp' , set tmp to new value
  5. Continue scrolling to last element and clicking show more, till current length== 'tmp'

       driver.manage().window().maximize();
           String baseUrl = "https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en_US&showAllReviews=true";
           driver.get(baseUrl);
           List<WebElement> a;
    
           //find all the reviews
           a= driver.findElements(By.cssSelector("span[jsname=\"bN97Pc\"]"));
           int b = 0;
           JavascriptExecutor js = (JavascriptExecutor) driver;
           WebElement showmore;
    
           //scroll to last element
           while(a.size()!=b) {
    
               b=a.size();
               System.out.println("size of b is "+ b); 
               js.executeScript("arguments[0].scrollIntoView();",  a.get(a.size() - 1));
               Thread.sleep(3000);
    
               Boolean isPresent = driver.findElements(By.xpath("//*[contains(text(),\"Show More\")]/../..")).size() > 0;
    
               if(isPresent) {
                   showmore= driver.findElement(By.xpath("//*[contains(text(),\"Show More\")]/../.."));
                   showmore.click();
                   Thread.sleep(2000);
               }
    
               a= driver.findElements(By.cssSelector("span[jsname=\"bN97Pc\"]"));
               System.out.println("size of b is "+ a.size());
               System.out.println(a.size()== b); 
           }
    
           for (WebElement review : a) {
                System.out.println(review.getText()); 
            }
    
1
  • Still one more issue is there, div tag having 2 span tags, if the review is short one span tag having text, if it crossed some limit, another span having it,.. for i in range(1, 40): if driver.find_element_by_xpath("//div[@jscontroller='LVJlx'][1]//button")[i].is_enabled(): reviewFullText = driver.find_elements_by_xpath("//div[@jscontroller='LVJlx']//span[2]") else: reviewFullText = driver.find_elements_by_xpath("//div[@jscontroller='LVJlx']//span[1]") for reviewText in reviewFullText: Reviews.append(reviewText.text) – Sivakumar Prakash Apr 8 '20 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.