以下是我要做的事情的片段,基本上只是将一个项目添加到购物车并使用Selenium webdriver转到结帐页面。
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
driver.find_element_by_xpath("//div[@id='exp-gridwall-wrapper']/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div[2]/p").click()
size_button = driver.find_element_by_css_selector(".exp-pdp-size-dropdown")
actions = ActionChains(driver)
actions.move_to_element(size_button).perform()
driver.find_element_by_id("buyingtools-add-to-cart-button").click()
checkout_button = driver.find_element_by_css_selector(".checkout_button")
actions = ActionChains(driver)
actions.move_to_element(checkout_button).perform()
我目前在步骤中收到此错误"点击添加到购物车":
WebDriverException: Message: Element is not clickable
我相信那之后的一切都被打破了......
我绊倒的部分是识别要点击的正确css元素。
如果有人能够解释我做错了什么或给我选择了正确的元素,以便我可以添加到购物车并点击结帐,那将非常感谢!
答案 0 :(得分:1)
我很确定这是因为你打开了尺寸下拉菜单并且它覆盖了“添加到购物车”按钮。
只需最大化浏览器窗口:
driver.maximize_window()
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
# ...
另外,按钮类名称中有一个拼写错误 - 它是checkout-button
而不是checkout_button
。而且,您需要添加Explicit Waits来解决可见性和时间问题:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
wait = WebDriverWait(driver, 10)
driver.find_element_by_xpath(
"//div[@id='exp-gridwall-wrapper']/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div[2]/p").click()
# opening size dropdown
size_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".exp-pdp-size-and-quantity-container a.exp-pdp-size-dropdown")))
actions = ActionChains(driver)
actions.move_to_element(size_button).click().perform()
# selecting size
size = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[contains(@class, 'nsg-form--drop-down--option') and normalize-space(.) = 'S']")))
actions = ActionChains(driver)
actions.move_to_element(size).click().perform()
# adding to cart
driver.find_element_by_id("buyingtools-add-to-cart-button").click()
# checkout
checkout_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".checkout-button")))
actions = ActionChains(driver)
actions.move_to_element(checkout_button).click().perform()
适合我。