Can't add any items to cart on bestbuy.ca but bestbuy.com works?

Viewed 2960

I've been trying to learn how to add items to my cart on BestBuy.ca. Unfortunately, whenever I attempt to add an item to my cart I get the following error: enter image description here

However, on the American version of the site, the exact same code (only modified classname) succeeds at adding items to the cart. Anyone know why this is happening?

Canadian site code:

from selenium import webdriver
import time

PATH = "C:/webdrivers/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.bestbuy.ca/en-ca/product/glue-loca-liquid-for-smart-phone-top-glass-lcd-screen-repairing-50g-optical-clear/10754680")

buyButton = driver.find_element_by_class_name("addToCartButton")
buyButton.click()

time.sleep(20)

American site code:

from selenium import webdriver
import time

PATH = "C:/webdrivers/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.bestbuy.com/site/artscase-tempered-glass-screen-protector-for-apple-iphone-12-pro-max-clear/6442159.p?skuId=6442159")

##Will ask which bestbuy site if you're not American so added 10s sleep to select the American site.
time.sleep(10)

buyButton = driver.find_element_by_class_name("add-to-cart-button")
buyButton.click()

time.sleep(20)
1 Answers

There is a bot detection feature based on navigator.webdriver. You have to hide it using a chrome option, so that the site doesn't detect that the browser is started by an automation process.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

import time

PATH = "C:/webdrivers/chromedriver.exe"

options = Options()
options.add_argument('--disable-blink-features=AutomationControlled')

driver = webdriver.Chrome(PATH,options=options)
driver.get("https://www.bestbuy.com/site/artscase-tempered-glass-screen-protector-for-apple-iphone-12-pro-max-clear/6442159.p?skuId=6442159")

##Will ask which bestbuy site if you're not American so added 10s sleep to select the American site.
time.sleep(10)

buyButton = driver.find_element_by_class_name("add-to-cart-button")
buyButton.click()

time.sleep(20)

Without the chrome option flag:

enter image description here

With the chrome option flag:

enter image description here

Related