i currently working on web scraping of data from the lazada site using selenium in python: https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325723972.html?spm=a2o42.seller.list.1.75895319pt8HKU&mp=1
However, i am only able to extract out the first page of the product review. Does anyone know how to extract review from page2?
Here are the codes (but there is error that Element is not clickable at point from the codes below):
from selenium import webdriver
from bs4 import BeautifulSoup as soup
import time
from selenium.webdriver.chrome.options import Optionsurl = 'https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325723972.html?spm=a2o42.seller.list.1.75895319pt8HKU&mp=1'
chrome_options = Options()
#chrome_options.add_argument("--headless")driver = webdriver.Chrome(executable_path='chromedriver',chrome_options=chrome_options)
driver.get(url)
time.sleep(0.1)review_csv=[]
product_csv = []
rating_csv =[]
date_review_csv = []titles = driver.find_element_by_class_name('pdp-mod-product-badge-title').text
print(titles)
product_reviews = driver.find_elements_by_css_selector("[class='item']")urls = []#Page 1 of product review
for product in product_reviews :review = product.find_element_by_css_selector("[class='content']").textif(review != "" or review.strip()):print(review)review_csv.append(review)else:print(review)review_csv.append("No comments/review is an image")#Product Purchase#Check if the product purchase existsproduct_purchase = product.find_element_by_css_selector("[class='skuInfo']").textprint(product_purchase)product_csv.append(product_purchase)#Star ratingstar_ratings = product.find_elements_by_css_selector("[class='star']")stars = "https://laz-img-cdn.alicdn.com/tfs/TB19ZvEgfDH8KJjy1XcXXcpdXXa-64-64.png"star_rate = 0for rating in star_ratings:#print(rating.get_attribute('src'))if(rating.get_attribute('src') == stars):star_rate = star_rate + 1rating_csv.append(star_rate)print(star_rate)# Date of Reviewdate = product.find_element_by_css_selector("[class='title right']").textdate_review_csv.append(date)print(date)#Page 2 of product review onwards
page2_product_reviews = driver.find_element_by_xpath('//*[@id="module_product_review"]/div/div[3]/div[2]/div/div/button[2]').click()
for product in page2_product_reviews :review = product.find_element_by_css_selector("[class='content']").textif(review != "" or review.strip()):print(review)review_csv.append(review)else:print(review)review_csv.append("No comments/review is an image")#Product Purchase#Check if the product purchase existsproduct_purchase = product.find_element_by_css_selector("[class='skuInfo']").textprint(product_purchase)product_csv.append(product_purchase)#Star ratingstar_ratings = product.find_elements_by_css_selector("[class='star']")stars = "https://laz-img-cdn.alicdn.com/tfs/TB19ZvEgfDH8KJjy1XcXXcpdXXa-64-64.png"star_rate = 0for rating in star_ratings:#print(rating.get_attribute('src'))if(rating.get_attribute('src') == stars):star_rate = star_rate + 1rating_csv.append(star_rate)print(star_rate)# Date of Reviewdate = product.find_element_by_css_selector("[class='title right']").textdate_review_csv.append(date)print(date)driver.close()
Thank you in advance!