How to get XPATH elements that have different endings?

Question:

I am trying to add each product to cart by going with the click over the product and then click the button add product to cart
from this site https://www.bershka.com/ro/femeie/accesorii/%C8%99osete-c1010194004.html

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time


options = Options()
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)
driver.get("https://www.bershka.com/ro/femeie/accesorii/%C8%99osete-c1010194004.html")


cookies_bttn = driver.find_element(By.ID, "onetrust-accept-btn-handler")
cookies_bttn.click()
driver.implicitly_wait(10)
country_save = driver.find_element(By.CSS_SELECTOR, "#geoblocking > div > div > div.select-country-container > button.button.is-sm.confirm")
country_save.click()
hoover = ActionChains(driver)

time.sleep(10)
pbody = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))

for x in range(5):
    pbody.send_keys(Keys.PAGE_DOWN)
    print('scrolled')
    time.sleep(1)
sosete = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="category-product-card"]')))
print(len(sosete))



for x in str(len(sosete)):
    ActionChains(driver).move_to_element(sosete).perform()
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".quick-purchase__detail__button"))).click()

Output: AttributeError: move_to requires a WebElement

I’ve tried many ways but errors pop up everytime and I can’t find any solution
I thought about making a for loop using XPATH but I dont know how to get each product because they have different li like so:
first product = /html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/section[1]/div/ul/li[1]/div
second product = /html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/section[1]/div/ul/li[2]/div
And so on

Asked By: Diana Stroescu

||

Answers:

Assuming the code from your previous answer, let actions be defined as:

actions = ActionChains(driver)

Depending on your geographical IP address, you might need:

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, '//span[@class="bskico-cancel-16"]'))).click()
    print('removed location popup')
except Exception as e:
    print('no location popup')

Assuming the items list as:

items = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="category-product-card"]')))

You add to your code:

for i in items:
    actions.move_to_element(i).perform()
    t.sleep(5)
    i.find_element(By.XPATH, './/span[@class="quick-purchase__detail__button__text"]').click()
    print('added to basket', i.text)
    t.sleep(5)

The hardcoded waiting times are there to account for network slowness, as well as server slowness.
Shopping basket accepts a maximum of 26 products, so you will not be able to add them all to it.

Answered By: Barry the Platipus