【问题标题】:How to get XPATH elements that have different endings?如何获取具有不同结尾的 XPATH 元素?
【发布时间】: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


【解决方案1】:

假设您之前答案中的代码,将 actions 定义为:

actions = ActionChains(driver)

根据您的地理 IP 地址,您可能需要:

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')

假设项目列表为:

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

您添加到您的代码:

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)

硬编码的等待时间是为了解决网络缓慢以及服务器缓慢的问题。 购物篮最多接受 26 件产品,因此您无法将它们全部添加到其中。

【讨论】:

  • 好的,但是还有什么方法可以将最多 26 种产品添加到购物车吗?我试过你的代码,它只添加了一个
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 2016-02-04
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多