Search code examples
pythonpython-3.xselenium-webdriverweb-scrapingexport-to-csv

how to scrap data from shopee.co.id


i cant seem to find the element for both username and password from shopee.co.id, can someone please help me :). im learning how to scrap data from the web. but i'm not sure how to tell python to input my email and password and click "login" because everytime i run the code, the page just want me to put my credentials.

here is my code :

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

class Scraper:
  def __init__(self):
    self.driver = webdriver.Firefox()

  def get_data(self):
    self.driver.get('https://shopee.co.id/sunxin_store?page=0&sortBy=sales')
    wait = WebDriverWait(self.driver, 30)
    wait.until(EC.element_to_be_clickable((By.ID, 'loginKey'))).send_keys("")
    wait.until(EC.element_to_be_clickable((By.ID, 'password'))).send_keys("")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

    counter_page = 0
    datas = []

    while counter_page < 5:
      for _ in range(0, 5000, 500):
        time.sleep(0.5)
        self.driver.execute_script("window.scrollBy(0,500)")

      elements = self.driver.find_elements(By.CLASS_NAME, 'col-xs-2 shop-collection-view__item')
      for element in elements:
        img = element.find_element(By.CLASS_NAME, 'wFlKK5').get_attribute()
        name = element.find_element(By.CLASS_NAME, 'h0HBrE ckHqor _5Kkvx1').text
        price = element.find_element(By.CLASS_NAME, '_0ZJOIv').text
        rating = element.find_element(By.CLASS_NAME, '_01uxti').text
        sale = element.find_element(By.CLASS_NAME, 'sPnnFI AGQcTN').text
        
        datas.append({
          'img': img,
          'name': name,
          'price': price,
          'sale' : sale,
          'rating' : rating,
          
        })
      
      counter_page += 1
      print("Scrapping data from shopee.co.id/sunxin_store page:", counter_page)
    try:
        next_page_xpath = "//button[@class='m2.5 11c .1 0 .2 0 .3-.1l6-5c .1-.1.2-.3.2-.4s-.1-.3-.2-.4l-6-5c-.2-.2-.5-.1-.7.1s-.1.5.1.7l5.5 4.6-5.5 4.6c-.2.2-.2.5-.1.7.1.1.3.2.4.2z' and text()='" + str(counter_page + 1) + "']"
        next_page = self.driver.find_element(By.XPATH, next_page_xpath)
        next_page.click()
        print("Navigating to page:", counter_page + 1)
    except NoSuchElementException:
        print("No more pages to navigate to.")

    return datas

Solution

  • The loginKey is @name attribute not @id.

    This is not correct: By.ID, 'loginKey'

    Change as below:

    wait.until(EC.element_to_be_clickable((By.NAME, 'loginKey'))).send_keys("")
    wait.until(EC.element_to_be_clickable((By.NAME, 'password'))).send_keys("")
    # Below line will click on LOG IN button
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Log in']"))).click()
    

    enter image description here