1

I can't pull the price of an item from a specific container using class name or XPATH. I tried pulling different html blocks but it just won't work. The price format should look like this ₱38 - ₱120. Here's the site https://shopee.ph/search?keyword=a4%20notebook

#all containers
container = driver.find_elements_by_xpath('//*[@class="_1gkBDw _2O43P5"]') #list
time.sleep(10)

#product 
item_list = []
price_list = []
for item in container: #individual container
     product = item.find_element_by_class_name('O6wiAW') #'//*[@class="O6wiAW"]' #specific container 
     item_list +=[product.text]
     price = item.find_element_by_class_name('_1w9jLI _37ge-4 _2ZYSiu') #MY PROBLEM
     price_list += [price.text]
     
print(price_list)
print(len(price_list))

print(item_list)
print(len(item_list))



exit(1)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

To print the price of the first item i.e. ₱38 - ₱120 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH:

    driver.get('https://shopee.ph/search?keyword=a4%20notebook')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row shopee-search-item-result__items']/div[@data-sqe='item']//span[text()='₱']//.."))).text)
    
  • Console Output:

    ₱38 - ₱120
    
  • 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
  • Thank you very much. – CringeMachine9000 Aug 17 '20 at 16:11
  • Sir, I'm sorry but it returns a repetitive price. Since I'm trying to extract the item from a specific container it just repeats the html on a specific xpath. – CringeMachine9000 Aug 18 '20 at 12:00
  • @CringeMachine9000 What do you mean by _...returns a repetitive price..._?. Did you read the answer carefully atleast once? I started my answer with _...price of the first item..._ and have used `visibility_of_element_located` which would always return a single definite element. So every time your program will locate the same element only. – undetected Selenium Aug 18 '20 at 12:09
  • `visibility_of_element_located` will suffice since I am getting only the element on a specific container `for item in container` not all the elements in the whole page. The problem is when I use By.XPATH it returns the same element over and over regardless of the item. However, that is not the case when I use the class name as shown in my code `product = item.find_element_by_class_name('O6wiAW')`. I'm sorry for troubling you good sir. – CringeMachine9000 Aug 19 '20 at 11:25