10.10.1.9考试系统代码完成一次答题代码备份
1 # coding=utf-8 2 from selenium import webdriver 3 from time import ctime,sleep 4 5 driver = webdriver.Chrome() 6 7 # 用户登录 8 def login(): 9 driver.get("http://10.10.1.9") 10 driver.find_element_by_id("username").send_keys("candy31") 11 driver.find_element_by_id("password").send_keys("1") 12 driver.find_element_by_id("loginbtn").click() 13 14 # 考试答题 15 def exam(): 16 # 进入考试 17 driver.get("http://10.10.1.9/super/pingnan/front/view.php?id=1669") 18 # 开始答题 19 driver.find_element_by_css_selector( 20 "#content_right > div:nth-child(2) > div > section > section > div.box.quizattempt > div.singlebutton.quizstartbuttondiv > form > div > input[type="submit"]:nth-child(1)").click() 21 # driver.find_element_by_xpath("//*[@id='content_right']/div[2]/div/section/section/div[2]/div[1]/form/div/input[1]").click() 22 # driver.find_element_by_id('q1518:1_answerfalse').click()#后续可以用正则尝试实现? 23 # 第一题-判断题 24 driver.find_element_by_css_selector("#q1 > div.content > div > div.ablock > div.answer > div.r1 > label").click() 25 driver.find_element_by_name("next").click() 26 # 提交答案 27 # 按钮组搜索 ,方法一点击“提交所有答案并结束” 28 # btns = driver.find_elements_by_css_selector("#content_right>div>div>div>div>form>div>input") 29 # for btn in btns: 30 # val = btn.get_attribute("value") 31 # if val == "提交所有答案并结束": 32 # btn.click() 33 # else: 34 # print(val) 35 # 方法二,点击input上方的div复制其css_selector路径(刷新页面,在div元素上没有变量的时候去复制),点击“提交所有答案并结束” 36 driver.find_element_by_css_selector("#content_right > div:nth-child(2) > div:nth-child(6) > div > div > form > div> input:nth-child(1)").click() 37 # 定位提交答案按钮 38 driver.find_element_by_css_selector("body>div>div>div>div>div>div>input:nth-child(1)").click() 39 40 41 # 主函数 42 if __name__ == '__main__': 43 login() 44 sleep(2) 45 exam() 46 sleep(2) 47 print("all over %s" % ctime())