Farhan Ahmed
Farhan Ahmed

Reputation: 192

how to get product price from different size selenium python

I want to scrape product information from this page . This product have three different size and price will be change if I select different size from drop-down section. Right now my scraper only can scrape default price after first time initially page load which is 35 for 1kg. How I will scrape price for 500g and 250g. here is my code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

#argument for incognito Chrome
#argument for incognito Chrome
option = Options()
option.add_argument("--incognito")


browser = webdriver.Chrome(options=option)
browser.get("https://boutique.cafebarista.ca/products/cremone?variant=18033418797121")

product_title = browser.find_element_by_xpath('//h1[@class="product-name"]')
long_description = browser.find_element_by_xpath('//div[@class="product-landing-container"]')
price=browser.find_element_by_xpath('//div[@class="product-btn-price ProductPrice"]')

print(product_title.text,long_description.text,price.text)


browser.quit()

Upvotes: 0

Views: 1430

Answers (2)

frianH
frianH

Reputation: 7563

With .find_elements_by_css_selector you can get each text without clicking the weight drop down first, this is the selector I mean:

nav[id="w-dropdown-list-16"] > a > div

And you can also click on each of these elements using .execute_script

Try following code:

driver.get('https://boutique.cafebarista.ca/products/autentico?variant=18033459331137')
weight_list = driver.find_elements_by_css_selector('nav[id="w-dropdown-list-16"] > a > div')
for weight in weight_list:
    driver.execute_script('arguments[0].click();', weight)
    price = driver.find_element_by_id('ProductPrice').text
    print(weight.get_attribute('innerHTML') +' ' +price)

Upvotes: 2

SeleniumUser
SeleniumUser

Reputation: 4177

Try below solution

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

browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
browser.maximize_window()
wait = WebDriverWait(browser, 20)

browser.get("https://boutique.cafebarista.ca/products/cremone?variant=18033418797121")

kg_button=browser.find_element_by_xpath("//div[@id='w-dropdown-toggle-16']")
kg_button.click()
list =wait.until(EC.presence_of_all_elements_located((By.XPATH, "//nav[@id='w-dropdown-list-16']//a")))
kg_button.click()
for element in list:
     kg_button.click()
     actionChains = ActionChains(browser)
     actionChains.move_to_element(element).click().perform()

     price = browser.find_element_by_xpath("//div[@id='ProductPrice']")
     print product_title.text
     print element.text
     print price.text


browser.quit()

Upvotes: 1

Related Questions