python - 添加到购物车时 selenium 失败,如何单击 javascript 按钮?

标签 python selenium-webdriver

这是我正在尝试做的事情的一个片段,它基本上只是将一个项目添加到购物车并使用 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 元素。

如果有人能解释我做错了什么或告诉我要选择的正确元素以便我可以添加到购物车并点击结帐,我们将不胜感激!

最佳答案

我很确定这是因为您打开了尺寸下拉菜单并且它覆盖了“添加到购物车”按钮。

只需最大化浏览器窗口:

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

对我有用。

关于python - 添加到购物车时 selenium 失败,如何单击 javascript 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990897/

相关文章:

关于 Selenium-Element 的 Python 3.7 帮助无法滚动到 View 中

python - Flask-Testing 模块没有这样的测试方法报错

python - 使用 python win32com 库发送 AppointmentIthem

c# - 通过Unity引用python脚本

python - 为 Microsoft Edge 使用 python selenium

java - 等待图像完全加载 Selenium WebDriver

python - 任何人都知道使 django-registration 使用电子邮件作为用户名的好方法吗?

python - 如何创建一个新的 python tkinter 窗口,使其完全适合可用的桌面区域,但未最大化?

python - 难以将不同页面的项目打印在一起

php - 使用来自 facebook 的 selenium 和 php webdriver 从下拉列表中选择选项