1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options 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.common.action_chains import ActionChains import time import random
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--log-level=3')
chromedriver_bin = r'D:\Plugs\ChromeDriver\126.0.6478.126\chrome-win64\chromedriver.exe' service = webdriver.ChromeService(executable_path=chromedriver_bin) driver = webdriver.Chrome(service=service, options=options)
wait = WebDriverWait(driver, 10)
driver.implicitly_wait(6)
login_url = 'http://x.x.x.x/' username = 'admin' password = 'pnet'
def pnetlab_login():
driver.get(login_url)
username_input = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@placeholder="Username"]'))) username_input.clear() username_input.send_keys(username)
password_input = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@placeholder="Password"]'))) password_input.clear() password_input.send_keys(password)
login_button = driver.find_element(By.XPATH, '//div[@class="button btn btn-info" and text()="Login"]') login_button.click()
try: search_element = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@placeholder="Search Labs"]'))) print("登录成功!") return True except Exception as e: print("登录失败或者无法找到搜索框元素.\n", str(e)) return False
def random_lab(): lab_names = ('H1 CFG SPOTO Ver_1.unl', 'H1 Plus CFG SPOTO Ver_1.unl', 'H2 CFG SPOTO CCIE RS Ver_1.unl', 'H2 Plus CFG SPOTO Ver_1.unl', "H3 SPOTO CCIE RS Ver_1.unl") chosen_lab = random.choice(lab_names) return chosen_lab
def main(): try: sign_in = pnetlab_login() if not sign_in: print('>>>登陆有问题') return
chosen_lab = random_lab() print('-' * 50 + f'\n本次抽中的lab是:{chosen_lab[:-4]}') search_element = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@placeholder="Search Labs"]'))) search_element.clear() search_element.send_keys(chosen_lab)
time.sleep(1) first_button = wait.until( EC.visibility_of_element_located((By.XPATH, "//div[@id='search_lab_suggest']/div[@class='button'][1]"))) first_button.click()
open_button = driver.find_element(By.CSS_SELECTOR, "div.box_flex > div.btn-info:nth-child(1)") open_button.click()
leftmenu_trigger = driver.find_element(By.CLASS_NAME, "logo_img")
actions = ActionChains(driver)
actions.move_to_element(leftmenu_trigger).perform()
setup_nodes = driver.find_element(By.XPATH, '//span[text()="Setup Nodes"]') setup_nodes.click()
start_all = driver.find_element(By.CLASS_NAME,"action-nodesstart") start_all.click()
actions.move_to_element(leftmenu_trigger).perform() system_status = driver.find_element(By.XPATH, '//span[text()="System Status"]') system_status.click()
print('-' * 50 + '\n正在启动设备\n')
x = 1 while x < 30: time.sleep(10) started = wait.until(EC.presence_of_element_located((By.XPATH, "//td/strong[@style='color: red;']")))
print("已启动设备数:", started.text) if int(started.text) >= 40: break x += 1 time.sleep(5) print(f'设备启动完成,打开链接{login_url},开始实验\n' * 3 + '-' * 50)
except Exception as e: print(e) print('>>>OPS,FUCKED UP') finally: driver.quit()
if __name__ == '__main__': main()
|