Issue
Im trying to loop through each item under the url: https://www.zara.com/es/en/man-jeans-l659.html?v1=2351397 Click add to cart (small + button on each item) then get the li's in the window that pop's up:
My code until now:
import time
options = Options()
#options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = uc.Chrome(options=options)
driver.get("https://www.zara.com/es/en/man-jeans-l659.htmlv1=2351397")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button[class = "zds-button geolocation-modal__button zds-button--primary"]'))).click()
buttons = driver.find_elements(By.CSS_SELECTOR, 'button[class = "product-add-to-cart__button"]')
for button in buttons:
button.click()
print(driver.find_elements(By.CSS_SELECTOR, "div.size-selector-list__wrapper size-selector-list__wrapper--grid size-selector-list__wrapper--open > ul.size-selector-list > li.size-selector-list__item"))
time.sleep(1)
Tried many ways but couldn't get the li items propertly.
Solution
- You should wait for the elements before interacting with them
- Your locator to /li elements doesn't find any elements. You can check it via dev tools (F12 in chrome > elements tab > Ctrl+F)
The following part of the code works ok for me:
actions = ActionChains(driver)
buttons = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'button.product-add-to-cart__button')))
for i, button in enumerate(buttons):
actions.move_to_element(button).click().perform()
sizes = wait.until(
EC.presence_of_all_elements_located(
(
By.CSS_SELECTOR,
'div.size-selector-list__wrapper--open > ul.size-selector-list > li div.product-size-info__main-label'
)
)
)
print(f"Product {i} sizes: {[x.get_attribute('innerHTML') for x in sizes]}")
Output:
Product 0 sizes: ['38', '40', '42', '44', '46']
Product 1 sizes: ['36', '38', '40', '42', '44']
Product 2 sizes: ['38', '40', '42', '44']
Product 3 sizes: ['36', '38', '40', '42', '44']
Product 4 sizes: ['36', '38', '40', '42', '44', '46']
Product 5 sizes: ['36', '38', '40', '42', '44', '46']
Product 6 sizes: ['36', '38', '40', '42', '44', '46']
Product 7 sizes: ['38', '40', '42', '44', '46']
Product 8 sizes: ['38', '40', '42', '44', '46']
Product 9 sizes: ['38', '40', '42', '44', '46']
Product 10 sizes: ['38', '40', '42', '44', '46']
Product 11 sizes: ['36', '38', '40', '42', '44', '46']
Product 12 sizes: ['36', '38', '40', '42', '44', '46']
Product 13 sizes: ['36', '38', '40', '42', '44', '46']
Product 14 sizes: ['36', '38', '40', '42', '44', '46']
Product 15 sizes: ['36', '38', '40', '42', '44', '46']
Product 16 sizes: ['36', '38', '40', '42', '44', '46']
Product 17 sizes: ['36', '38', '40', '42', '44', '46']
Product 18 sizes: ['36', '38', '40', '42', '44', '46']
Product 19 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 20 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 21 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 22 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 23 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 24 sizes: ['36', '38', '40', '42', '44', '46', '48']
Product 25 sizes: ['36', '38', '40', '42', '44', '46', '48']
Answered By - sashkins
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.