I’ve recently had to upgrade a large number of Avocent console servers using GUI and it was an awfully boring experience. Because i don’t like such repetitive work, I decided that it’s high time I learnt how to automate mindless clicking.
Install Python, pip, the selenium library, and the pyautogui library. Detailed steps depend on your OS.
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 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import pyautogui #this ensures that you don't need to deal with the https self-signed-certificate issue options = webdriver.ChromeOptions() options.add_argument('ignore-certificate-errors') browser = webdriver.Chrome(chrome_options=options) #here I go to my console server, find the username field (to find the ID, press F12 in your browser to open devtools and find the right GUI element in the html code. Mind you, it might take you a while to find the right element. ), and type in 'myadminusername' browser.get('https://192.168.1.1') username = browser.find_element_by_id("username") username.send_keys("myadminusername") #I find the password field (and then waste an hour because the password field is non-interactable so I can't send keys in any way) password = browser.find_element_by_id("password").click() #so i deal with this problem in a different way. The field is active because i clicked on it so i can just type away... webdriver.ActionChains(browser).key_down(Keys.SHIFT).send_keys("m").perform() webdriver.ActionChains(browser).key_up(Keys.SHIFT).send_keys("y").perform() webdriver.ActionChains(browser).key_down(Keys.SHIFT).send_keys("p").perform() webdriver.ActionChains(browser).key_up(Keys.SHIFT).send_keys("a").perform() webdriver.ActionChains(browser).send_keys("s").perform() webdriver.ActionChains(browser).send_keys("s").perform() webdriver.ActionChains(browser).send_keys(Keys.RETURN).perform() #now i'm in the actual GUI, I click on a menu element on the lefthandside. Here I use the find_by_css_selector method instead of find_element_by_id browser.find_element_by_css_selector("a[onclick*=overview]").click(); #I execute some js code instead of clicking on an option browser.execute_script("XML_request(\"units.overview\",\"upgradeFirmware\");") #I find a radio button to select how i want to select the file used for the upgrade. Finding by xpath is yet another way of finding an element. browser.find_element_by_xpath("//input[@value='from-mycomputer']").click() #I select the option "Choose from file" which will open the file explorer to locate the file browser.find_element_by_id("fileXferForm").click() #now i can't use selenium anymore because Selenium only works in the browser and now i have File Explorer window open and i need to find the upgrade file on my PC. #I need to use pyautogui to move the mouse and click pyautogui.position() pyautogui.FAILSAFE = False #i move the mouse cursor to where I want it to be to click on firmware download (later) pyautogui.moveTo(900, 300, duration =4) #i type in the path where my upgrade file is and hit ENTER pyautogui.typewrite('F:\\my_upgrade_file\n', interval=0) #now i click on the DOWNLOAD button (because the cursor is in the right place) pyautogui.click(clicks=2, interval=2, button='left') #now the file download will take a moment, after which I need to click again on the Install button. This is #TODO because i haven't worked out yet how to deal with the changing download time before I can click. Of course I can click 100 times with an interval of 3 seconds but...