4
\$\begingroup\$

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")
\$\endgroup\$
6
  • \$\begingroup\$ It's fine to obscure your credentials, but please don't obscure the original 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\$ Commented Nov 19, 2020 at 16:51
  • \$\begingroup\$ Unfortunately the URL is not accessible outside the organisation network. \$\endgroup\$ Commented Nov 19, 2020 at 19:48
  • 1
    \$\begingroup\$ In that case could you edit your post to include a sample of the HTML that is being returned by the URL? \$\endgroup\$ Commented Nov 19, 2020 at 19:51
  • \$\begingroup\$ Appreciate where you guys are coming from. This platform and the eqtiquette is all new to me. Programming - HTML / Python is not my background. I literally studied "Automate Boring Stuff" earlier this week and managed to write this script. I'll need to figure out how to extract the website and upload it somewhere for users. \$\endgroup\$ Commented Nov 19, 2020 at 19:54
  • \$\begingroup\$ You don't appear to actually authenticate to the website, which points toward using Requests instead of Selenium. \$\endgroup\$ Commented Nov 19, 2020 at 22:51

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.