Can you help me use Selenium to click Add To Cart button?

柔情痞子 提交于 2021-02-05 09:26:57

问题


I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Add To Cart" button using either find_element_by_class or find_element_by_XPATH. The problem is to check if the item is out of stock, and if it is out of stock then to refresh the webpage and restart the script. If the item is in stock then it should click "Add To Cart"

I am using:

Python v3.9

Chrome v87

This is the URL i am practicing on:

https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html

And this is my current code for the clicking:

# Selenium Tutorial #1

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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

import time

driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")

driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")

#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)

link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click

try:
    element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
    )
    element.click()
except Exception:
    pass

driver.implicitly_wait(2)

try:
    element = WebDriverWait(driver, 1).until(
    EC.presence_of_element_located((By.XPATH, "email-desktop"))
    )
    time.sleep(1)
    browser.refresh()
except:
    driver.find_element_by_class_name("Button__StyledButton-bvTPUF hZIOeU Button-jyKNMA GZkwS")
    link.click()


回答1:


You cannot pass multiple classes to find_element_by_class_name. Instead use driver.find_element_by_css_selector or xpath.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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

import time

driver = webdriver.Chrome(r"C:\Users\Frank\Documents\Python Scripts\chromedriver_win32\chromedriver.exe")

driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")

#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)

try:
    element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
    )
    element.click()
except Exception:
    pass

driver.implicitly_wait(2)

try:
    element = WebDriverWait(driver, 1).until(
    EC.presence_of_element_located((By.XPATH, "email-desktop"))
    )
    time.sleep(5)
    driver.refresh()
except:
    button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")

Next, if you would call button.is_displayed() it will return False, because it is a hidden element. This means you cannot interact with it using button.click(). Instead, you can use javascript to click the button.

driver.execute_script("arguments[0].click();", button)


来源:https://stackoverflow.com/questions/65768940/can-you-help-me-use-selenium-to-click-add-to-cart-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!