├── README.md └── instrumentRreservation.py /README.md: -------------------------------------------------------------------------------- 1 | # InstrumentReservation 2 | ### 自动化实验仪器抢购预约 3 | 4 | ### 项目介绍 5 | * 【技术背景】本项目使用 python 语言进行编写,自动化框架采用 selenium 来进行实现 6 | * 【开发背景】因为仪器预约这个步骤比较机械化,如果每天都需要手动去预约会给很多同学造成不必要的时间开销,这部门完全可以交给代码去完成,本项目的代码只是针对特定学校,如果需要对自己学校的实验预约进行自动化预约需求做个性化的修改 7 | * 【声明】项目是在我 2021 年同学遇到这个问题的时候随手帮忙写的,前后花费的时间可能在 2-3 个小时,所以有些地方可能并没有做很好的处理,还有很大的优化空间 8 | 9 | ### 使用方式 10 | * 因为本项目是基于 python 语言的,所以需要操作系统配置好相关的 python 环境 11 | * 建议在服务器上面运行,将 py 文件拷贝到云服务器进行运行,也可以使用自己的电脑,通过 ```python instrumentRreservation.py``` 来运行 12 | * 运行过程中不要关闭窗口 13 | 14 | ## 如果可以的话,希望能够一起学习,请联系我 sinhao@live.com 15 | 16 | 17 | -------------------------------------------------------------------------------- /instrumentRreservation.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from webdriver_manager.chrome import ChromeDriverManager 3 | from selenium.webdriver import ActionChains 4 | from selenium.webdriver.support.select import Select 5 | import time 6 | 7 | times = 0 8 | sleep_time = 1 9 | 10 | # 此处设置需要设置的日期 11 | start_index = 16 12 | end_index = 18 13 | day = 9 14 | web = "此处输入相对应的预约网址,eg.http://yqgx.zcmu.edu.cn/lims/" 15 | username = "此处输入 username" 16 | password = "此处输入 password" 17 | 18 | ### 控制时间 19 | # strat_time = "2021-12-" + str(day) + " " + str(start_index) + ":00:01" 20 | # end_time = "2021-12-" + str(day) + " " + str(end_index) + ":00:00" 21 | # print(strat_time) 22 | # print(end_time) 23 | # stimeArray = time.strptime(strat_time, "%Y-%m-%d %H:%M:%S") 24 | # etimeArray = time.strptime(end_time, "%Y-%m-%d %H:%M:%S") 25 | # start_timestamp = time.mktime(stimeArray) 26 | # end_timestamp = time.mktime(etimeArray) 27 | # print(end_timestamp - start_timestamp) 28 | # print("h") 29 | 30 | # option=webdriver.ChromeOptions() 31 | # option.add_argument('headless') # 设置option 32 | # driver = webdriver.Chrome(chrome_options=option) # 调用带参数的谷歌浏览器 33 | driver = webdriver.Chrome(ChromeDriverManager().install()) 34 | 35 | # driver = webdriver.Chrome("chromedriver") 36 | driver.get(web) 37 | 38 | driver.find_element_by_xpath("//*[@id='sidebar']/div/div/div[2]/div[3]/div/h1/a").click() 39 | 40 | driver.find_element_by_id("username").clear() 41 | driver.find_element_by_id("username").send_keys(username) 42 | driver.find_element_by_id("password").clear() 43 | driver.find_element_by_id("password").send_keys(password) 44 | driver.find_element_by_xpath("//*[@id='fm1']/table/tbody/tr[4]/td/input").click() 45 | print("ok") 46 | # 点击首页预约 47 | driver.find_element_by_xpath("//*[@id='table_equipments_follow_equipments']/tbody/tr[1]/td[2]/div/div/a").click() 48 | time.sleep(2) 49 | # 此处为了解决加载慢的问题 50 | # 自动判断"添加"按钮是否加载完成 51 | # for i in range(200): 52 | # try: 53 | # el = driver.find_element_by_link_text("添加") 54 | # if el.is_displayed(): 55 | # break 56 | # except: 57 | # pass 58 | # time.sleep(0.1) 59 | # else: 60 | # print("无法找到\"添加\"元素,TimeOut!") 61 | while True: 62 | ### 控制时间 63 | strat_time = "2021-12-" + str(day) + " " + str(start_index) + ":00:01" 64 | end_time = "2021-12-" + str(day) + " " + str(end_index) + ":00:00" 65 | stimeArray = time.strptime(strat_time, "%Y-%m-%d %H:%M:%S") 66 | etimeArray = time.strptime(end_time, "%Y-%m-%d %H:%M:%S") 67 | start_timestamp = time.mktime(stimeArray) 68 | end_timestamp = time.mktime(etimeArray) 69 | above = driver.find_element_by_link_text("添加") 70 | ActionChains(driver).double_click(above).perform() 71 | time.sleep(2) 72 | 73 | # 将焦点放到新的元素上面 74 | # 此次有新的窗口弹出,将焦点聚集到新弹出的小页面上 75 | a = driver.find_element_by_class_name('dialog_content') 76 | driver.execute_script('arguments[0].click()',a) 77 | 78 | time.sleep(0.5) 79 | Target_Div = driver.find_element_by_name("project_lab") 80 | driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div) 81 | time.sleep(0.5) 82 | sel = driver.find_element_by_name("project_lab") 83 | Select(sel).select_by_index(1) 84 | time.sleep(0.2) 85 | 86 | start_val = driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/form/table/tbody/tr[4]/td[2]/input[1]") 87 | driver.execute_script("arguments[0].value = \'"+ str(int(start_timestamp)) +"\';", start_val) 88 | 89 | start_val = driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/form/table/tbody/tr[4]/td[2]/input[2]") 90 | driver.execute_script("arguments[0].value = \'"+ str(int(start_timestamp)) +"\';", start_val) 91 | 92 | end_val = driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/form/table/tbody/tr[5]/td[2]/input[1]") 93 | driver.execute_script("arguments[0].value = \'"+ str(int(end_timestamp)) +"\';", end_val) 94 | 95 | end_val = driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/form/table/tbody/tr[5]/td[2]/input[2]") 96 | driver.execute_script("arguments[0].value = \'"+ str(int(end_timestamp)) +"\';", end_val) 97 | time.sleep(0.2) 98 | Target_Div2 = driver.find_element_by_name("project") 99 | driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div2) 100 | time.sleep(1) 101 | sel = driver.find_element_by_name("project") 102 | time.sleep(2) 103 | try: 104 | Select(sel).select_by_value("510") 105 | except: 106 | print("选择510,此处发生错误!") 107 | time.sleep(0.2) 108 | driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/form/table/tbody/tr[9]/td/div/input").click() 109 | 110 | # driver.find_element_by_xpath("//*[@id='tr_lab_6188c40f2f90a']/td[2]/div/div").click() 111 | # aa = driver.find_element_by_link_text("--") 112 | # aa = driver.find_element_by_xpath("# aa = driver.find_element_by_class_name('dropdown_text')") 113 | # aa = driver.find_element_by_class_name("text autoselect").text 114 | # print(aa) 115 | # ActionChains(driver).double_click(aa).perform() 116 | # bb = driver.find_element_by_link_text("XXX课题组") 117 | # ActionChains(driver).double_click(bb).perform() 118 | # 将焦点放到新的元素上面 119 | time.sleep(1) 120 | b = driver.find_element_by_class_name('dialog_title') 121 | driver.execute_script('arguments[0].click()', b) 122 | try: 123 | time.sleep(2) 124 | e1 = driver.find_element_by_class_name("dialog_title") 125 | if e1.is_displayed(): 126 | # 预约失败,可能是时间未到 127 | times += 1 128 | print("正在进行第【{}】次尝试,时间【{}】".format(times, time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))) 129 | driver.find_element_by_class_name("select").click() 130 | time_h = time.localtime().tm_hour 131 | time_m = time.localtime().tm_min 132 | time_s = time.localtime().tm_sec 133 | now_time = "2021-12-" + str(day) + " " + str(time_h) + ":"+ str(time_m) +":"+ str(time_s) + "" 134 | end_time = "2021-12-" + str(day) + " " + str(end_index) + ":00:01" 135 | print("预约时间为【{}】,现在时间为【{}】".format(end_time, now_time)) 136 | stimeArray = time.strptime(end_time, "%Y-%m-%d %H:%M:%S") 137 | end_timestamp = time.mktime(stimeArray) 138 | stimeArray2 = time.strptime(now_time, "%Y-%m-%d %H:%M:%S") 139 | now_timestamp = time.mktime(stimeArray2) 140 | sleep_time = end_timestamp - now_timestamp 141 | #现在还没有到预约的时间 142 | if sleep_time > 0: 143 | print("1.还没到时间,预约【{}】还需要等待【{}】秒".format(end_time, sleep_time)) 144 | time.sleep(sleep_time) 145 | else: 146 | continue 147 | time.sleep(2) 148 | continue 149 | else: 150 | print("2021-12-{} {}:00:01-{}:00:00预约成功,成功时间为【{}】".format(day,start_index, end_index,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))) 151 | start_index += 2 152 | end_index += 2 153 | if end_index == 24: 154 | day += 1 155 | start_index = 8 156 | end_index = 10 157 | print("全天预约结束,等待8小时候继续预约!") 158 | sleep_time = 60 * 60 * 8 - 60 159 | sleep_time(sleep_time) 160 | else: 161 | time_h = time.localtime().tm_hour 162 | time_m = time.localtime().tm_min 163 | time_s = time.localtime().tm_sec 164 | now_time = "2021-12-" + str(day) + " " + str(time_h) + ":" + str(time_m) + ":" + str(time_s) + "" 165 | end_time = "2021-12-" + str(day) + " " + str(end_index) + ":00:01" 166 | stimeArray = time.strptime(end_time, "%Y-%m-%d %H:%M:%S") 167 | end_timestamp = time.mktime(stimeArray) 168 | stimeArray2 = time.strptime(now_time, "%Y-%m-%d %H:%M:%S") 169 | now_timestamp = time.mktime(stimeArray2) 170 | sleep_time = end_timestamp - now_timestamp 171 | if sleep_time > 0: 172 | print("2.预约【{}】等待【{}】秒".format(end_time, sleep_time)) 173 | time.sleep((sleep_time)-2) 174 | except: 175 | pass 176 | time.sleep(2) 177 | print("Success, OK!") 178 | --------------------------------------------------------------------------------