New to Python/Selenium and learning on the fly with my first basic selenium script.
Request user input - activate or deactivate. Open's website, loops through shop URL as well as activate/deactivate checkboxes.
Would be grateful for some feedback on how to optimise the code.
# IMPORTS
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# X PATH LOCATIONS
shop = '//*[@id="zz2_TopNavigationMenun1"]/table/tbody/tr/td/a'
product_range = '//*[@id="zz1_QuickLaunchMenun2"]/td/table/tbody/tr/td/a'
product_sale = '//*[@id="divBdyGroup_WG_Main"]/table/tbody/tr[2]/td/a'
product_record = '//*[@id="Enregistrer_HolWebArticlesCaisse_ctl00_m_g_a26e7d54_b496_4e0f_8176_7ddab403a0c0"]'
# WEB MAG URL
webmag_login = 'URL'
webmag_url = 'URL'
# CONTROLS
shopcodes = ['251' ,'257' ,'268' ,'271' ,'285' ,'411' ,'495' ,'787' ,'794' ,'838' ,'842' ,'847' ,'852' ,'857' ,'977' ,'978' ,'979' ,'A74' ,'A75' ,'A77' ,'A78' ,'B08' ,'C01' ,'C02' ,'D14' ,'D93' ,'E41' ,'E42' ,'E50' ,'G38' ,'G52' ,'G84' ,'G85' ,'G95' ,'R20' ,'R67'] # SHOP CODES TO CHECK
plucodes = ["2432"] # PLU CODES TO CHECK
# VARIABLES
prompt = input("Do you want to \'activate\' or \'deactivate\' items?\n\n")
# OPTIONS
chrome_options = Options()
# chrome_options.add_argument("--kiosk")
driver = webdriver.Chrome(options=chrome_options)
##################################################
print("\n[ --------------- ACTIVE PRODUCT CHECK ------------]\n\nShop codes to check :")
print(shopcodes)
print("\nPLU codes to check :")
print(plucodes)
print("\n[ ------------- STARTING MAIN SCRIPT --------------]\n")
##################################################
def webmag_launch_login():
print(">>> launch web driver")
driver.get(webmag_login)
# driver.fullscreen_window()
print(">>> open and login to webmag\n\n[ ------------ ITERATE THROUGH SHOPS --------------]\n")
time.sleep(1)
def webmag_nav_to_range_tick():
for Y in shopcodes:
driver.get(webmag_url + Y)
print(Y + " - Login to Store")
time.sleep(1)
driver.find_element_by_xpath(product_range).click()
time.sleep(1)
driver.find_element_by_xpath(product_sale).click()
time.sleep(1)
print("[ - CHECKING ITEMS -- ]")
for X in plucodes: # iterate through PLU CODES
try:
checkbox = "//td[div=\'" + X + "\']/following-sibling::td/input[@type=\'checkbox\']"
productname = "//td[div=\'" + X + "\']/following-sibling::td[1]"
result = driver.find_element_by_xpath(checkbox).is_selected()
if result:
print(X + " - " + driver.find_element_by_xpath(productname).text + "\tPLU - already ticked")
# print(driver.find_element_by_xpath(productname).text)
else:
driver.find_element_by_xpath(checkbox).click()
print(X + " - " + driver.find_element_by_xpath(productname).text + "\tPLU - ticked")
time.sleep(1)
except:
print(X + "- PLU - not found")
time.sleep(1)
# driver.find_element_by_xpath(shop).click()
# SAVE BUTTON
# driver.find_element_by_xpath(product_record).click()
def webmag_nav_to_range_untick():
for Y in shopcodes:
driver.get(webmag_url + Y)
print(Y + " - Login to Store")
time.sleep(1)
driver.find_element_by_xpath(product_range).click()
time.sleep(1)
driver.find_element_by_xpath(product_sale).click()
time.sleep(1)
print("[ - CHECKING ITEMS -- ]")
for X in plucodes: # iterate through PLU CODES
try:
checkbox = "//td[div=\'" + X + "\']/following-sibling::td/input[@type=\'checkbox\']"
productname = "//td[div=\'" + X + "\']/following-sibling::td[1]"
result = driver.find_element_by_xpath(checkbox).is_selected()
if result:
print(X + " - " + driver.find_element_by_xpath(productname).text + "\tPLU - unticked")
driver.find_element_by_xpath(checkbox).click()
else:
print(X + " - " + driver.find_element_by_xpath(productname).text + "\tPLU - already unticked")
time.sleep(1)
except:
print(X + "- PLU - not found")
time.sleep(1)
# driver.find_element_by_xpath(shop).click()
# SAVE BUTTON
# driver.find_element_by_xpath(product_record).click()
# EXECUTE DEF
if prompt == "activate":
webmag_launch_login()
webmag_nav_to_range_untick()
elif prompt == "deactivate":
webmag_launch_login()
webmag_nav_to_range_tick()
else:
print("Script Ended")
print("Script Finished Running")
webmag_url. Reviewers being able to access the site will be able to more meaningfully comment on the best way to interact with it. \$\endgroup\$