【发布时间】:2022-11-22 04:24:53
【问题描述】:
我正在尝试通过单击产品将每个产品添加到购物车,然后单击按钮将产品添加到购物车 从这个网站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:\webdrivers\chromedriver.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()
输出:AttributeError:move_to 需要一个 WebElement
我尝试了很多方法,但每次都会弹出错误,我找不到任何解决方案 我考虑过使用 XPATH 创建一个 for 循环,但我不知道如何获取每个产品,因为它们有不同的 li,如下所示: 第一个产品 = /html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/section[1]/div/ul/li[1]/div 第二个产品 = /html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/section[1]/div/ul/li[2]/div 等等
【问题讨论】:
-
为什么不做
for i in range(totalProds)..然后通过 xpath...li[{i}]....找到要点击的产品? f strings tutorial如果你需要的话
标签: python selenium web-scraping selenium-chromedriver