[英]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.现在我的刮板只能在第一次初始页面加载后刮默认价格,即 35 公斤。 How I will scrape price for 500g and 250g.我将如何刮 500g 和 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()
With .find_elements_by_css_selector
you can get each text without clicking the weight
drop down first, this is the selector I mean:使用.find_elements_by_css_selector
您无需先单击weight
下拉菜单即可获取每个文本,这是我的意思的选择器:
nav[id="w-dropdown-list-16"] > a > div
And you can also click on each of these elements using .execute_script
您还可以使用.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)
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.