from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import os
import pickle #保存和读取cookies实现免登陆工具
import time
import datetime
from selenium.webdriver.support.ui import Select
cs_url = "http://kygx.ahmu.edu.cn/"
tem_url = "http://kygx.ahmu.edu.cn/lfsms/personbook/timeadd?insid=66322&f=person&c=lfsmspersonbooktimeadd"
sem_url = "http://kygx.ahmu.edu.cn/lfsms/personbook/timeadd?insid=66323&f=person&c=lfsmspersonbooktimeadd"
what = input("请输入电镜:TEM or SEM:")
if what == "TEM":
url = tem_url
value_1 = "611"
else:
url = sem_url
value_1 = "633"
class Concert:
def __init__(self):
self.status = 0
self.login_method = 1
s = Service("chromedriver.exe")
self.driver = webdriver.Chrome(service=s)
def set_cookie(self):
self.driver.get(cs_url)
sleep(40)
pickle.dump(self.driver.get_cookies(), open("cookies.pkl", "wb"))
self.driver.get(url)
def get_cookie(self):
try:
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
cookie_dict = {
'domin': 'kygx.ahmu.edu.cn',
'name': cookie.get('name'),
'value': cookie.get('value')
}
self.driver.add_cookie(cookie_dict)
print('###载入Cookie###')
except Exception as e:
print(e)
def login(self):
if self.login_method == 0:
self.driver.get(cs_url)
print("###开始登陆###")
elif self.login_method == 1:
if not os.path.exists('cookies.pkl'):
self.set_cookie()
else:
self.driver.get(cs_url)
self.get_cookie()
self.driver.get(url)
def enter_concert(self):
print('###打开浏览器###')
self.login()
self.driver.refresh()
self.status =2
print('###登陆成功###')
def choice_time(self):
js = '$("#startTime").val("2023-05-12 08:30:00");$("#endTime").val("2023-05-12 09:30:00");$("#FC-Form").submit();'
self.driver.execute_script(js)
s1 = Select(self.driver.find_element(By.ID, "CAR_Tbookingrecord_BillingProFileId"))
s1.select_by_value(value_1)
self.driver.find_element(By.XPATH, '//*[@id="CAR_Tbookinghassample_SampleName"]').send_keys("合成纳米材料")
self.driver.find_element(By.XPATH, '//*[@id="CAR_Tbookinghassample_SampleCount"]').send_keys("3")
s2 = Select(self.driver.find_element(By.ID, "CAR_Tbookinghassample_SampleFormID"))
s2.select_by_value("283")
s3 = Select(self.driver.find_element(By.ID, "CAR_Tbookinghassample_SampleSourceID"))
s3.select_by_value("1302")
#保存按钮
self.driver.find_element(By.XPATH, '//*[@id="formID"]/div[44]/div[2]/button').click()
def re_net(self):
self.driver.refresh()
def qiang(self):
while True:
sleep(2)
self.driver.find_element(By.XPATH, '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[3]/td/div/button').click()
self.driver.find_element(By.XPATH, '//*[@id="formID"]/div[44]/div[2]/button').click()
self.driver.refresh()
time.sleep(1)
s1 = Select(self.driver.find_element(By.ID, "CAR_Tbookingrecord_BillingProFileId"))
s1.select_by_value(value_1)
self.driver.find_element(By.XPATH, '//*[@id="CAR_Tbookinghassample_SampleName"]').send_keys("合成纳米材料")
self.driver.find_element(By.XPATH, '//*[@id="CAR_Tbookinghassample_SampleCount"]').send_keys("3")
s2 = Select(self.driver.find_element(By.ID, "CAR_Tbookinghassample_SampleFormID"))
s2.select_by_value("283")
s3 = Select(self.driver.find_element(By.ID, "CAR_Tbookinghassample_SampleSourceID"))
s3.select_by_value("1302")
self.driver.find_element(By.XPATH, '//*[@id="formID"]/div[44]/div[2]/button').click()
if __name__ == '__main__':
try:
con = Concert()
con.enter_concert()
cs_time = input("请输入最早开始抢的预约时间:如,16:00:00###(在这个点准时提交表单):")
while True:
time_now = time.strftime("%H:%M:%S", time.localtime())
print(time_now)
if time_now == cs_time:
con.choice_time()
con.qiang()
break
else:
con.re_net()
except Exception as e:
print(e)