Selenium 无法定位元素 Best Buy Prices
Selenium Cannot Locate Element Best Buy Prices
我目前正在学习 Selenium 并希望在 BestBuy 上练习。我无法弄清楚如何通过 XPATH 或使用 Selenium 的 CLASSNAME 来获取价格。
我做错了什么?我不断收到的错误是 Selenium 无法找到该元素。有人能告诉我为什么 Selenium 找不到元素吗?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(r"Desktop\Selenium\chromedriver.exe")
website = 'https://www.bestbuy.com/site/apple-10-2-inch-ipad-latest-model-with-wi-fi-64gb-space-gray/4901809.p?skuId=4901809'
driver.get(website)
title = driver.find_element(By.CLASS_NAME,'sku-title')
price = driver.find_element(By.XPATH,'//*[@id="pricing-price-2768792"]/div/div/div/div/div/div[1]/div[1]/div/div/span[1]')
status = driver.find_element(By.CLASS_NAME,'fulfillment-add-to-cart-button')
status = status.text
print(price.text)
主要问题是属性的后缀是动态的,会随着时间或请求的变化而变化。
试着改变你的选择策略,找到一些不太动态的东西来实现你的目标。例如。选择 css selectors
:
driver.find_element(By.CSS_SELECTOR,'[data-sticky-media-gallery] .priceView-hero-price span')
注意: 结果也取决于您要从中抓取的国家/地区,因此我必须为美国单击 link,才能获得最终结果产品页面
driver.find_element(By.CLASS_NAME,'us-link').click()
例子
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(r"Desktop\Selenium\chromedriver.exe")
website = 'https://www.bestbuy.com/site/apple-10-2-inch-ipad-latest-model-with-wi-fi-64gb-space-gray/4901809.p?skuId=4901809'
driver.get(website)
title = driver.find_element(By.CLASS_NAME,'sku-title')
price = driver.find_element(By.CSS_SELECTOR,'[data-sticky-media-gallery] .priceView-hero-price span')
status = driver.find_element(By.CLASS_NAME,'fulfillment-add-to-cart-button')
status = status.text
print(price.text)
输出
9.99
我目前正在学习 Selenium 并希望在 BestBuy 上练习。我无法弄清楚如何通过 XPATH 或使用 Selenium 的 CLASSNAME 来获取价格。
我做错了什么?我不断收到的错误是 Selenium 无法找到该元素。有人能告诉我为什么 Selenium 找不到元素吗?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(r"Desktop\Selenium\chromedriver.exe")
website = 'https://www.bestbuy.com/site/apple-10-2-inch-ipad-latest-model-with-wi-fi-64gb-space-gray/4901809.p?skuId=4901809'
driver.get(website)
title = driver.find_element(By.CLASS_NAME,'sku-title')
price = driver.find_element(By.XPATH,'//*[@id="pricing-price-2768792"]/div/div/div/div/div/div[1]/div[1]/div/div/span[1]')
status = driver.find_element(By.CLASS_NAME,'fulfillment-add-to-cart-button')
status = status.text
print(price.text)
主要问题是属性的后缀是动态的,会随着时间或请求的变化而变化。
试着改变你的选择策略,找到一些不太动态的东西来实现你的目标。例如。选择 css selectors
:
driver.find_element(By.CSS_SELECTOR,'[data-sticky-media-gallery] .priceView-hero-price span')
注意: 结果也取决于您要从中抓取的国家/地区,因此我必须为美国单击 link,才能获得最终结果产品页面
driver.find_element(By.CLASS_NAME,'us-link').click()
例子
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(r"Desktop\Selenium\chromedriver.exe")
website = 'https://www.bestbuy.com/site/apple-10-2-inch-ipad-latest-model-with-wi-fi-64gb-space-gray/4901809.p?skuId=4901809'
driver.get(website)
title = driver.find_element(By.CLASS_NAME,'sku-title')
price = driver.find_element(By.CSS_SELECTOR,'[data-sticky-media-gallery] .priceView-hero-price span')
status = driver.find_element(By.CLASS_NAME,'fulfillment-add-to-cart-button')
status = status.text
print(price.text)
输出
9.99