python 如何使用 selenium 设置亚马逊产品页面的大小?

zujrkrfu  于 2023-03-16  发布在  Python
关注(0)|答案(2)|浏览(149)

我做了一个python程序,我想设置产品的大小,以点击添加到购物车按钮。因此,启用我应该设置大小。我如何使用 selenium 键设置大小?此外,我想这个程序的工作产品,不需要设置大小,这个程序是为这类产品(例如this product
所以我想设置的大小,使添加到购物车。代码应该在此行(driver.get(url))。我附加的程序。我将感谢任何帮助。

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys

proxies = {
    'http': 'http://217.119.82.14:8080',
    'https': 'http://196.27.107.30:8080',
}

url = "https://www.amazon.com/Disney-Stitch-Surfer-Adult-T-shrt/dp/B072VPQ1BG/ref=sr_1_4?ie=UTF8&qid=1517916607&sr=8-4&keywords=t+shrt"

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxies)

driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
                          chrome_options=chrome_options)
driver.get(url)

driver.find_element_by_xpath('//*[@id="submit.add-to-cart"]/span/input').click()
m1m5dgzv

m1m5dgzv1#

您可以通过id直接获得项目大小,但首先需要单击大小菜单

sizemenu = driver.find_element_by_id('dropdown_selected_size_name')
sizemenu.click()
select = driver.find_element_by_id('size_name_1') #Medium size
select.click()

检查页面上是否有可添加的菜单
x一个一个一个一个x一个一个二个x

zengzsys

zengzsys2#

要设置所需的大小,可以使用选择类:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get(url)
# Create new object for drop down
select = Select(driver.find_element_by_id("native_dropdown_selected_size_name"))
# Select "Small" size
select.select_by_visible_text("Small")
wait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//input[@id="add-to-cart-button" and not(@style="cursor: not-allowed;")]'))).click()

相关问题