Skip to content

Instantly share code, notes, and snippets.

@harnerdesigns
Created January 15, 2020 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harnerdesigns/491dcd1131213e0fcf88e4a4416ffaa8 to your computer and use it in GitHub Desktop.
Save harnerdesigns/491dcd1131213e0fcf88e4a4416ffaa8 to your computer and use it in GitHub Desktop.
A python script that runs through a CSV file of coupon code IDs to set "Exclude This Coupon From Cart-Level Discounts" on the BigCommerce Backend
import time
import os
import csv
#### Selenium to run Headless Chrome ####
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
#### tkinter to ask for CSV File ####
from tkinter import Tk
from tkinter.filedialog import askopenfilename
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=<PATH TO CHROME PROFILE>")
# download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads and put it in the
# current directory
service = Service('C:\\WebDriver\\bin\\chromedriver.exe')
service.start()
driver = webdriver.Remote(service.service_url, options=chrome_options)
def setCodes(driver, code):
editURL = "https://www.shoolu.com/manage/marketing/coupons/" + str(code)+"/edit"
driver.get(editURL)
element_present = EC.presence_of_element_located(
(By.ID, "content-iframe"))
WebDriverWait(driver, 5).until(element_present)
driver.switch_to.frame(driver.find_element_by_id("content-iframe"))
try:
element_present = EC.presence_of_element_located(
(By.ID, "excludeDiscounts"))
WebDriverWait(driver, 5).until(element_present)
finally:
enable = driver.find_element_by_id("excludeDiscounts")
print(enable.get_attribute("checked"))
if enable.get_attribute("checked") != 'true':
print("Checking Box")
enable.send_keys(' ')
driver.find_element_by_name("SaveButton1").click()
WebDriverWait(driver, 5).until(
lambda driver: driver.current_url != editURL)
#### LOG IN TO WEBSITE ####
driver.get("https://shoolu.com/manage")
try:
username = driver.find_element_by_id("user_email")
username.clear()
username.send_keys("USERNAME")
password = driver.find_element_by_id("user_password")
password.clear()
password.send_keys("PASSWORD")
driver.find_element_by_name("commit").click()
except:
print("Already Logged In")
#### GET CSV FILE OF CODE IDs ####
Tk().withdraw()
filename = askopenfilename() # Fetch File
print(filename)
with open(filename, 'r') as selectedFile:
next(selectedFile) # Skip Header Row
try:
for row in selectedFile:
rows = row.split(",") # Turn Each Row Into It's Own List
print(rows[0])
try:
setCodes(driver, rows[0])
except:
print("Error Setting")
finally:
driver.quit() # Kill Browser Window After Everything is Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment