1

I am trying to make a auto buy for bestbuy products. I have been looking at tutorials to solve the recaptcha using the recaptcha audio button.

So far I have been able to go to the product link. Click add to cart. Go to cart. Go to checkout. Type in email and password and click signin. Now it will take me to do a recaptcha.

From what I read in selenium docs you must first store iframe web element then switch to the selected iframe and then click on the button in the iframe. But for some reason I am not able to click it. I have been following some examples like example1 and example2 but still not managing it.

Here is what I have so far:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time

options = webdriver.ChromeOptions() 
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
#options.add_experimental_option("excludeSwitches", ["enable-logging"])

driver = webdriver.Chrome(options=options)
driver.maximize_window()

url1 = 'https://www.bestbuy.ca/en-ca/product/amd-ryzen-5-3600-hexa-core-3-6ghz-am4-desktop-processors/15331711'

driver.get(url1)

#addbutton = driver.find_element(By.CSS_SELECTOR, ".addToCartButton")
addbutton = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".addToCartButton")))
        
        
# add to cart
addbutton.click()

print("done adding to cart.. going to signin to checkout now")
        
time.sleep(7)

        
# go to cart
driver.get("https://www.bestbuy.ca/en-ca/basket")

time.sleep(7)

#go to checkout
driver.get("https://www.bestbuy.ca/identity/en-ca/signin?tid=ALeWh7xW3cQ0am2jxPwy%252Bb76uAf3U3S%252FK5kHOt9tXQBdoY3PBPeJZKOnNTI%252BivRSagy1iR3OqazLv%252FDVz33tFdYmNy1NQ5j8XeYNIN%252FeT7HAjsTaVL17znPtcdTyDoY5JsRqidRnIk%252BYGGCG3UKd08fStGLDP3c5gGfENH0sSYglvA%252FdWi1%252BOcpVnGARPpGU2zXiHmeKGfT5NWuMzrOeP57BVjRW6xUfqvAOCRuAio2%252BrBnaNv8hzcIKdEWhZP90QwciiVZTVtl%252BInN8x9bG18AVgIW8AWmfcE8axkpOXJ2GRq1X9PIHlJl122IR8NEpE9nP84GCmMM8kUWCju9Ecsv0RCxmf3o8eByCWoK2HIWiNkMnHCH2EEikSX9xojCG")

print("arrived at signin")
        
#user = driver.find_element_by_id("username")
user = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "username")))
user.send_keys("thisismyemail@gmail.com")
time.sleep(0.1)

    
time.sleep(1)
        
password = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "password")))
    
 

password.send_keys("password")
time.sleep(0.15)

print("entered username and password")
    
time.sleep(1.25)



signInBtn = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div[3]/div/div/div/div[1]/div/div/form/div/button")))
time.sleep(3)
signInBtn.click()

print("signed in.")

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name^='c-xsgh29r4he3'][src^='https://www.google.com/recaptcha/api2/bframe?hl=en-GB&v=bfvuz6tShG5aoZp4K4zPVf5t&k=6LdLhXEUAAAAACZo2QdzqcnoSN7FvEZT8pMykY2s&cb=f835b2sax2o7']")))


print("test")
driver.switch_to_default_content()
audiobutton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div/div[3]/div[2]/div[1]/div[1]/div[2]/button")))
time.sleep(3)
audiobutton.click()

print("clicked.")

1 Answers1

1

This change made it so I was able to click the captcha-audio-button.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time

options = webdriver.ChromeOptions() 
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
#options.add_experimental_option("excludeSwitches", ["enable-logging"])

driver = webdriver.Chrome(options=options)
driver.maximize_window()

url1 = 'https://www.bestbuy.ca/en-ca/product/amd-ryzen-5-3600-hexa-core-3-6ghz-am4-desktop-processors/15331711'

driver.get(url1)

#addbutton = driver.find_element(By.CSS_SELECTOR, ".addToCartButton")
addbutton = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".addToCartButton")))
        
        
# add to cart
addbutton.click()

print("done adding to cart.. going to signin to checkout now")
        
time.sleep(7)

        
# go to cart
driver.get("https://www.bestbuy.ca/en-ca/basket")

time.sleep(7)

#go to checkout
driver.get("https://www.bestbuy.ca/identity/en-ca/signin?tid=ALeWh7xW3cQ0am2jxPwy%252Bb76uAf3U3S%252FK5kHOt9tXQBdoY3PBPeJZKOnNTI%252BivRSagy1iR3OqazLv%252FDVz33tFdYmNy1NQ5j8XeYNIN%252FeT7HAjsTaVL17znPtcdTyDoY5JsRqidRnIk%252BYGGCG3UKd08fStGLDP3c5gGfENH0sSYglvA%252FdWi1%252BOcpVnGARPpGU2zXiHmeKGfT5NWuMzrOeP57BVjRW6xUfqvAOCRuAio2%252BrBnaNv8hzcIKdEWhZP90QwciiVZTVtl%252BInN8x9bG18AVgIW8AWmfcE8axkpOXJ2GRq1X9PIHlJl122IR8NEpE9nP84GCmMM8kUWCju9Ecsv0RCxmf3o8eByCWoK2HIWiNkMnHCH2EEikSX9xojCG")

print("arrived at signin")
        
#user = driver.find_element_by_id("username")
user = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "username")))
user.send_keys("username@gmail.com")
time.sleep(0.1)

    
time.sleep(1)
        
password = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "password")))
    
 

password.send_keys("password")
time.sleep(0.15)

print("entered username and password")
    
time.sleep(1.25)



signInBtn = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div[3]/div/div/div/div[1]/div/div/form/div/button")))
time.sleep(3)
signInBtn.click()

print("signed in.")

try:    
   # driver.switch_to.frame('c-xsgh29r4he3')
   frame = driver.find_element_by_xpath("//iframe[@title='recaptcha challenge']")
   print("made it!")
   driver.switch_to.frame(frame)
   print("we made it!")

   time.sleep(5)
   

   #audiobutton = driver.find_element_by_xpath("/html/body/div/div/div[3]/div[2]/div[1]/div[1]/div[2]/button")
   audiobutton = driver.find_element_by_id("recaptcha-audio-button")
   audiobutton.click()   


   
   print("complete")
finally:
    test = ""