0

I am learning selenium and Python. I can not select class name for price, class name = '_30jeq3 _1_WHN1'. My code:

import os
import selenium 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

os.environ['PATH'] += r"C:/Users/bekon/Desktop/scrapping/selenium"
driver = webdriver.Chrome()

driver.get(" https://www.flipkart.com/laptops/pr?sid=6bo%2Cb5g&p%5B%5D=facets.brand%255B%255D%3DLenovo&p%5B%5D=facets.type%255B%255D%3DGaming%2BLaptop&ctx=eyJjYXJkQ29udGV4dCI6eyJhdHRyaWJ1dGVzIjp7InRpdGxlIjp7Im11bHRpVmFsdWVkQXR0cmlidXRlIjp7ImtleSI6InRpdGxlIiwiaW5mZXJlbmNlVHlwZSI6IlRJVExFIiwidmFsdWVzIjpbIkxlbm92byBHYW1pbmcgTGFwdG9wcyJdLCJ2YWx1ZVR5cGUiOiJNVUxUSV9WQUxVRUQifX19fX0%3D&wid=4.productCard.PMU_V2_2 ")
driver.maximize_window()
driver.implicitly_wait(8)

What I can do is selection the class above the price, class name = '_3tbKJL':

price = driver.find_elements(By.CLASS_NAME, '_3tbKJL')

What I want to do is to select the price class name = '_30jeq3 _1_WHN1'

price = driver.find_elements(By.CLASS_NAME, ''_30jeq3 _1_WHN1')

I have been trying already many option and can not solve it.

Was trying for example:

price = driver.find_elements(By.CLASS_NAME, '_3tbKJL')

The out put then is: ₹76,890 ₹53,490 ₹88,09039% off Free delivery ₹59,990 ₹93,69035% off Free delivery ₹1,30,990 ... And was trying to make indexing trough the first component.

for i in price:
    p = i[0]   
    print(p.text)

Results in:

TypeError: 'WebElement' object is not subscriptable
  1. Why does the indexing does not work?
  2. How the select the class for the price.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
MTPy
  • 1
  • 1

1 Answers1

0

The classnames which you are trying to use e.g. _3tbKJL, _30jeq3 are dynamically generated and would change sooner or later.


Solution

To print the prices i.e. ₹76,890, ₹53,490, etc you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategy:

driver.get('https://www.flipkart.com/laptops/pr?sid=6bo%2Cb5g&p%5B%5D=facets.brand%255B%255D%3DLenovo&p%5B%5D=facets.type%255B%255D%3DGaming%2BLaptop&ctx=eyJjYXJkQ29udGV4dCI6eyJhdHRyaWJ1dGVzIjp7InRpdGxlIjp7Im11bHRpVmFsdWVkQXR0cmlidXRlIjp7ImtleSI6InRpdGxlIiwiaW5mZXJlbmNlVHlwZSI6IlRJVExFIiwidmFsdWVzIjpbIkxlbm92byBHYW1pbmcgTGFwdG9wcyJdLCJ2YWx1ZVR5cGUiOiJNVUxUSV9WQUxVRUQifX19fX0%3D&wid=4.productCard.PMU_V2_2')
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class, '_1_WHN1') and starts-with(., '₹')]")))])
driver.quit()

Console Output:

['₹76,890', '₹53,490', '₹59,990', '₹1,30,990', '₹73,990', '₹74,590', '₹56,990', '₹72,490', '₹64,390', '₹60,990', '₹74,999', '₹1,57,990', '₹76,490', '₹70,990', '₹77,990', '₹1,42,990', '₹62,990', '₹74,990', '₹58,999', '₹74,790', '₹1,15,990', '₹2,49,990', '₹88,990', '₹82,000']

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352