1

I'm trying to create a bot to add a 4k tv to my cart on BestBuy as a way to self teach myself selenium. I am having trouble with associating my 'element' variable to the button on BestBuy's website. I've been looking through some forums and cannot find a solution that works. I found the element through the HTML of the BestBuy website.

Here is an image of what I think is finding the element in chrome: chrome on BestBuy

And here is my python code(excuse the sloppiness of my code I just started learning Selenium today):

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.bestbuy.ca/en-ca/product/samsung-75-4k-uhd-hdr-led-tizen-smart-tv-un75tu6900fxzc-2020-titan-grey-only-at-best-buy/14930256")
while True:
    if ("Add to Cart" in driver.page_source):
        print("In stock")
        element = driver.find_element_by_id("button_2Xgu4 primary_oeAKs.addToCartButton_1DQ8z.addToCartButton.regular_cDhX6")
        element.click()
        break
    else:
        print("Not in stock")
    driver.get("https://www.bestbuy.ca/en-ca/product/samsung-75-4k-uhd-hdr-led-tizen-smart-tv-un75tu6900fxzc-2020-titan-grey-only-at-best-buy/14930256");
        
    
    

1 Answer 1

1

You used find_element_by_id, and you put class name instead of the id of the element. However the element doesn't have any id, so you have to use find_element_by_class_name or find_element_by_css_selector. I copied your code and I used class name. That didnt work, so I used css selector. And that worked.

Here you go:

element = driver.find_element_by_css_selector("div[class='addToCartLabel_1eyxz']")
element.click()

You can learn more about finding elements Here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.