# coding=utf-8
import sys
import os
import time
import datetime
import traceback
from threading import Thread
from selenium import webdriver 
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
TEST_RANGE=[
list(range(1,5)),
list(range(5,27)),
list(range(27,30)),
list(range(30,31)),
list(range(31,36)),
]
TEST_ANS=[
['D','A','A','B','A','A','B','B','B','A','B','A','B','A','A','B','A','B','D','A','D','A','A','D','C','C','A','A','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
['D','A','A'],
]

class Robert(object):
	def __init__(self,usrid="LQ0518585",password='123456',city='FoShan'):
		self.driver = None
		self.userid = usrid
		self.passwd = password
		self.city= city
		
	def startChrome(self):
		try:
			self.driver = webdriver.Chrome()
		except Exception as e:
			print(e)
			self.driver=None
			print("start Chrome failed, exit after 20 sec!")
			time.sleep(20)
			sys.exit()
			
	'''登陆
	1：打开主页
	2：选择列表
	3：输入用户和密码
	4：点击登陆
	'''
	def login(self):
		self.driver.get('http://www.iabe.cn/Index.aspx')
		time.sleep(2)
		self.driver.implicitly_wait(3)
		self.select_city(self.city)
		self.driver.find_element_by_id("ctl00_ContentPlaceHolder1_txtUserName").send_keys(self.userid)
		self.driver.find_element_by_id("ctl00_ContentPlaceHolder1_txtPassword").send_keys(self.passwd)
		self.driver.find_element_by_id("ctl00_ContentPlaceHolder1_btnlogin").click()
		
	'''导航到
	练兵场
	'''
	def goto_lianbingchang(self):
		self.driver.get('http://www.iabe.cn/study/king.aspx?subject=2')
		time.sleep(5)

	'''导航到
	1:-><span>驾驶证和机动车管理规定</span>
	2:-><span>道路通行条件及通行规定</span>
	3:-><span>道路交通安全违法行为及处罚</span>
	4:-><span>机动车基础知识</span>
	'''
	def get_knowledge(self,number):
		print("get_knowledge"+str(number))
		js='getKnowledge('+str(number)+');'
		self.driver.execute_script(js)
		time.sleep(2)
	'''导航到
	<span>驾驶证和机动车管理规定</span>
	1:->驾驶证申领和使用规定1
	2:->驾驶证申领和使用规定2
	3:->驾驶证申领和使用规定3
	4:->机动车登记和使用规定
	'''
	def do_knowledge_test(self,knowledgeNo):
		print("do_knowledge_test"+str(knowledgeNo))
		path="//a[contains(@href,'KnowledgeNo="+str(knowledgeNo)+"')]"
		self.driver.find_element_by_xpath(path).click()
		time.sleep(3)
		for ans in TEST_ANS[knowledgeNo-1]:
			self.answer(ans)
			time.sleep(3)
		self.jiaojuan2()
		time.sleep(2)
		self.goto_lianbingchang()

	def jiaojuan2(self):
		print("submit the answers")
		js='JiaoJuan2();'
		self.driver.execute_script(js)
		
	def answer(self,ans):
		print("answer the question")
		if ans == 'A':
			self.driver.find_element_by_xpath('//*[@id="totalCon"]/div[2]/div[1]/label[1]').click()
		elif ans == 'B':
			self.driver.find_element_by_xpath('//*[@id="totalCon"]/div[2]/div[1]/label[2]').click()
		elif ans == 'C':
			self.driver.find_element_by_xpath('//*[@id="totalCon"]/div[2]/div[1]/label[3]').click()
		elif ans == 'D':
			self.driver.find_element_by_xpath('//*[@id="totalCon"]/div[2]/div[1]/label[4]').click()
		else:
			print("ans is outof range(A,D)")
				
	'''从列表中选择一个城市'''
	def select_city(self,city):
		city_code={
					'FoShan':'1',
					'ShaoGuan':'2',
					'JiangMen':'3',
					'CangZhou':'4',
					'YuLin':'5',
					'JieYang':'6',
					'ZhaoQing':'7',
					'ZhanJiang':'8',
					'HeZhou':'9'
				 }
		self.driver.find_element_by_xpath("//button[@data-id='ctl00_ContentPlaceHolder1_selcitycode']").click()
		time.sleep(1)
		path = "//ul/li[@rel='"+city_code[city]+"']"
		self.driver.find_element_by_xpath(path).click()
	''' 
	逐个进入下面的
	1:-><span>驾驶证和机动车管理规定</span>
		逐个 1-4 答题
	2:-><span>道路通行条件及通行规定</span>
	3:-><span>道路交通安全违法行为及处罚</span>
	4:-><span>机动车基础知识</span>
	'''
	def run(self):
		for test in list(range(1,6)):
			self.get_knowledge(test)
			for kn in TEST_RANGE[test-1]:
				self.do_knowledge_test(kn)

if __name__=="__main__":
	r=Robert()
	r.startChrome()
	r.login()
	r.goto_lianbingchang()
	r.run()
	time.sleep(1000)