为特定元素引入 WebDriverWait
Inducing WebDriverWait for specific elements
这个问题是我上一个问题 (). I'm working on scraping all of the Air Jordan Data off of grailed.com (https://www.grailed.com/designers/jordan-brand/hi-top-sneakers) 的后续问题。我将尺寸、型号、url 和图像 url 存储在一个对象中。我目前有一个程序可以滚动浏览整个提要并获取所有这些。除了找到图像 url 之外,一切正常。图像 URL 似乎需要引起明确的等待,这是 @KunduK 建议的。我正在尝试实施他的解决方案,以便我可以在我的 for 循环中提取每个图像:
while True and len(sneakers) < sneaker_count:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Get sneakers currently on page and add to sneakers list
feed = driver.find_elements_by_class_name('feed-item')
images = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".feed-item .listing-cover-photo>img")))
for item in feed:
...
目前代码一次获取一组图像。我正在尝试在“for item in feed”块中获取图像。我想要 images = WebDriverWait(driver, 10).until(EC.visibility_of_elements_located((By.SOME SELECTOR", ???)))
之类的东西,但我真的不知道如何使用 'item' 元素找到它们。有人可以帮忙吗?
使用 and python you have to induce for visibility_of_all_elements_located()
and you can use either of the following 从每个图像中抓取图像 url:
使用CSS_SELECTOR
:
driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers')
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.product-card-container")))])
使用XPATH
:
driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers')
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='product-card-container']")))])
控制台输出:
['https://www.grailed.com/products/57773-jordan-brand-air-jordan-1-retro-high-og-court-purple', 'https://www.grailed.com/products/57803-jordan-brand-air-jordan-1-retro-high-og-obsidian', 'https://www.grailed.com/products/57759-jordan-brand-air-jordan-1-retro-high-og-2017-royal', 'https://www.grailed.com/products/57760-jordan-brand-air-jordan-1-retro-high-og-2018-shadow', 'https://www.grailed.com/products/59036-jordan-brand-air-jordan-4-retro-og-2019-bred', 'https://www.grailed.com/products/115772-jordan-brand-jordan-1-retro-high-og-pine-green', 'https://www.grailed.com/products/57817-jordan-brand-air-jordan-1-retro-high-og-shattered-backboard-3-0', 'https://www.grailed.com/products/61668-jordan-brand-travis-scott-travis-scott-x-air-jordan-4-retro-cactus-jack', 'https://www.grailed.com/products/114979-jordan-brand-air-jordan-1-retro-high-og-unc-to-chi', 'https://www.grailed.com/products/97122-jordan-brand-air-jordan-1-retro-high-og-fearless', 'https://www.grailed.com/products/97133-jordan-brand-air-jordan-11-bred-2019', 'https://www.grailed.com/products/61725-jordan-brand-air-jordan-4-retro-cool-grey', 'https://www.grailed.com/products/57762-jordan-brand-air-jordan-1-retro-high-og-banned-2016-banned-bred', 'https://www.grailed.com/products/87098-jordan-brand-travis-scott-travis-scott-x-air-jordan-6-retro-olive', 'https://www.grailed.com/products/57768-jordan-brand-air-jordan-1-retro-high-og-bred-toe', 'https://www.grailed.com/products/112831-jordan-brand-air-jordan-1-retro-high-og-royal-toe', 'https://www.grailed.com/products/111383-jordan-brand-air-jordan-4-retro-black-cat-2020', 'https://www.grailed.com/products/58136-jordan-brand-travis-scott-travis-scott-x-air-jordan-1-retro-high-og-mocha', 'https://www.grailed.com/products/57825-jordan-brand-air-jordan-1-retro-high-og-turbo-green', 'https://www.grailed.com/products/111377-jordan-brand-off-white-air-jordan-5-retro-sp-muslin']
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这个问题是我上一个问题 (
while True and len(sneakers) < sneaker_count:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Get sneakers currently on page and add to sneakers list
feed = driver.find_elements_by_class_name('feed-item')
images = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".feed-item .listing-cover-photo>img")))
for item in feed:
...
目前代码一次获取一组图像。我正在尝试在“for item in feed”块中获取图像。我想要 images = WebDriverWait(driver, 10).until(EC.visibility_of_elements_located((By.SOME SELECTOR", ???)))
之类的东西,但我真的不知道如何使用 'item' 元素找到它们。有人可以帮忙吗?
使用 visibility_of_all_elements_located()
and you can use either of the following
使用
CSS_SELECTOR
:driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers') print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.product-card-container")))])
使用
XPATH
:driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers') print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='product-card-container']")))])
控制台输出:
['https://www.grailed.com/products/57773-jordan-brand-air-jordan-1-retro-high-og-court-purple', 'https://www.grailed.com/products/57803-jordan-brand-air-jordan-1-retro-high-og-obsidian', 'https://www.grailed.com/products/57759-jordan-brand-air-jordan-1-retro-high-og-2017-royal', 'https://www.grailed.com/products/57760-jordan-brand-air-jordan-1-retro-high-og-2018-shadow', 'https://www.grailed.com/products/59036-jordan-brand-air-jordan-4-retro-og-2019-bred', 'https://www.grailed.com/products/115772-jordan-brand-jordan-1-retro-high-og-pine-green', 'https://www.grailed.com/products/57817-jordan-brand-air-jordan-1-retro-high-og-shattered-backboard-3-0', 'https://www.grailed.com/products/61668-jordan-brand-travis-scott-travis-scott-x-air-jordan-4-retro-cactus-jack', 'https://www.grailed.com/products/114979-jordan-brand-air-jordan-1-retro-high-og-unc-to-chi', 'https://www.grailed.com/products/97122-jordan-brand-air-jordan-1-retro-high-og-fearless', 'https://www.grailed.com/products/97133-jordan-brand-air-jordan-11-bred-2019', 'https://www.grailed.com/products/61725-jordan-brand-air-jordan-4-retro-cool-grey', 'https://www.grailed.com/products/57762-jordan-brand-air-jordan-1-retro-high-og-banned-2016-banned-bred', 'https://www.grailed.com/products/87098-jordan-brand-travis-scott-travis-scott-x-air-jordan-6-retro-olive', 'https://www.grailed.com/products/57768-jordan-brand-air-jordan-1-retro-high-og-bred-toe', 'https://www.grailed.com/products/112831-jordan-brand-air-jordan-1-retro-high-og-royal-toe', 'https://www.grailed.com/products/111383-jordan-brand-air-jordan-4-retro-black-cat-2020', 'https://www.grailed.com/products/58136-jordan-brand-travis-scott-travis-scott-x-air-jordan-1-retro-high-og-mocha', 'https://www.grailed.com/products/57825-jordan-brand-air-jordan-1-retro-high-og-turbo-green', 'https://www.grailed.com/products/111377-jordan-brand-off-white-air-jordan-5-retro-sp-muslin']
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC