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:
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");
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.