2021年1月17日星期日

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

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()    
https://stackoverflow.com/questions/65768940/can-you-help-me-use-selenium-to-click-add-to-cart-button January 18, 2021 at 12:43PM

没有评论:

发表评论