Automatic checkout with selenium

Question:

I am trying to get a automatic checkout code for this website (you will need to go to here and then add a random item to cart in order to access the checkout page)

I have used Selenium IDE to record and export the process of filling out credit card information and checking out.
The exported code looks as following :

    self.driver.get("https://www.adidas.co.il/en/checkout#Global-e_International_Checkout")
    self.driver.switch_to.frame(2)
    self.driver.switch_to.frame(0)
    self.driver.find_element(By.ID, "cardNum").click()
    self.driver.find_element(By.ID, "cardNum").send_keys("4444 4444 4444 4444")
    self.driver.find_element(By.ID, "cardExpiryMonth").click()
    dropdown = self.driver.find_element(By.ID, "cardExpiryMonth")
    dropdown.find_element(By.XPATH, "//option[. = '08']").click()
    self.driver.find_element(By.ID, "cardExpiryYear").click()
    dropdown = self.driver.find_element(By.ID, "cardExpiryYear")
    dropdown.find_element(By.XPATH, "//option[. = '2031']").click()
    self.driver.find_element(By.ID, "cvdNumber").click()
    self.driver.find_element(By.ID, "cvdNumber").send_keys("111")
    self.driver.switch_to.default_content()
    self.driver.find_element(By.ID, "btnPay").click()

When executing on Selenium IDE, it works perfectly and flawlessly, but when executing with PyCharm, I get a "no such frame" error, after the code reaches

self.driver.switch_to.frame(0)

I have also tried to remove self.driver.switch_to.frame(2) but then the code stopped at self.driver.find_element(By.ID, "cardNum").click() with a error code of "no such element"

Also, I have attempted to use WebDriverWait, but without success.

Any help will be highly appreciated

Asked By: Glitch

||

Answers:

The following code will dismiss the cookie button, (you may want to place that into a try/Except block, in case you don’t get the ‘Accept cookies’ popup) go through the process of selecting an item, etc, go to checkout and enter the details, up to cc number – you can continue and fill out the rest of the info. It’s using Firefox/geckodriver on Linux, however you should be able to adapt it to your own setup, just observe the imports and the part after defining the browser/driver:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t

firefox_options = Firefox_Options()

# firefox_options.add_argument("--width=1500")
# firefox_options.add_argument("--height=500")
# firefox_options.headless = True

driverService = Service('chromedriver/geckodriver')

browser = webdriver.Firefox(service=driverService, options=firefox_options)

url = 'https://www.adidas.co.il/en'

browser.get(url) 

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='affirm btn btn-primary btn-inline']"))).click()

t.sleep(2)

browser.get('https://www.adidas.co.il/en/GY9136.html')
t.sleep(2)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='42 2/3']"))).click()
t.sleep(2)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='add-to-cart btn btn-primary ready']"))).click()
t.sleep(2)
browser.get('https://www.adidas.co.il/en/checkout#Global-e_International_Checkout')  
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Checkout']")))
t.sleep(2)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='CheckoutData_BillingFirstName']"))).send_keys('Harry')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='CheckoutData_BillingLastName']"))).send_keys('Styles')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='CheckoutData_Email']"))).send_keys('[email protected]')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='CheckoutData_BillingAddress1']"))).send_keys('Tel Aviv, Eight street, 245')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='BillingZIP']"))).send_keys('2405918')
city_sel = Select(WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//select[@id='BillingCity']"))))
city_sel.select_by_visible_text('Abirim') 
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@id='secureWindow']")))
print('switched to checkout frame')
t.sleep(2)
cc_field = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//form[@id='paymentFrm']//input[@id='cardNum']")))
t.sleep(3)
cc_field.send_keys('234234342234234243')
t.sleep(2)
browser.switch_to.default_content()
print('switched back to main content')
t.sleep(2)
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Checkout']")))
t.sleep(2)
pay_button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//div[@id='paymentButtonBox']//button[@id='btnPay']")))
t.sleep(2)
print(pay_button.location_once_scrolled_into_view)
t.sleep(1)
pay_button.click()
print('clicked the pay button')

Selenium docs: https://www.selenium.dev/documentation/
EDIT: Updated the code, so it will also click the "pay’ button.

Answered By: platipus_on_fire