【问题标题】:Python Selenium add to cartPython Selenium 添加到购物车
【发布时间】:2023-03-23 18:45:01
【问题描述】:

我正在尝试使用 Selenium Python 添加到购物车。当我运行代码时,网站显示添加到购物车,然后出现错误。当我进入购物车页面时,它只是说没有物品添加到购物车中。我在这里做错了吗?

我的代码如下:

#add to cart
driver.find_element_by_xpath('//form[@action="/cart/add"]/input[@class="add cart-btn clearfix"]').click()

HTML如下:

<div class="product-form-wrapper">
 <form action="/cart/add" method="post" data-money-format="£{{amount}}" id="product-form-4424062369869" class="product-form" >
  <div id="product-variants" class="">
   <div class="select-wrapper">
    <select id="product-select" name="id" class="">  
     <option value="31655905919053">Medium</option>      
     <option value="31655906017357">Large</option>
     <option value="31655906148429">X-Large</option>
    </select>
   </div>
  </div>

  <input type="submit" name="button" class="add cart-btn clearfix" data-text="Add to Cart" value="Add to Cart" />

【问题讨论】:

  • 请包含错误和完整的 html 或指向它的链接
  • 在加入购物车之前可能必须选择尺码?
  • 正如@Farhad 提到的,在添加到购物车之前可能存在选择尺寸的情况......所以,我建议您在点击添加到购物车之前和之后使用一些屏幕截图来跟踪该过程点击添加到购物车。这是 CMD:driver.save_screenshot('/path-to-save-the-screenshot')。

标签: python-2.7 selenium selenium-webdriver


【解决方案1】:

对不起那个。请在此处找到网络链接:https://shop.palaceskateboards.com/products/lgylt0oiopch

这是我的代码

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException


#tell location of the webdriver and call it 'driver'
driver = webdriver.Firefox()

#go to website
driver.get('https://shop.palaceskateboards.com/collections/t-shirts')

#click on element
driver.find_element_by_xpath("//*[@id='product-loop']/div[19]").click()                  

#wait for sizedropdown
wait=WebDriverWait(driver, 10)
something=wait.until(EC.presence_of_element_located((By.ID, 'product-select')))

#select size
Select(driver.find_element_by_id('product-select')).select_by_visible_text('Large')

#add to cart
driver.find_element_by_xpath('//form[@action="/cart/add"]/input[@class="add cart-btn clearfix"]').click()

#go to checkout
driver.get('https://shop.palaceskateboards.com/cart')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多