selenium,unittest——驾照科目一网上答题自动化
需求很简单,所有题目全选A,然后点提交出分,校验是否到达出分这步
遇到的坑有这几个,一个是assertIn哪个是校验哪个是文本要分清,还有code的编码统一到Unicode,最后就是xpath定位各个题目的A的路径还不是完全一样,需要catch有问题的进行调整
#encoding=utf-8
from selenium import webdriver
import time,unittest
from seleniummon.exceptions import NoSuchElementException
'''
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#此方法是将Python2的默认编码ASCII改为 utf-8
'''
class Testwenquan(unittest.TestCase):
'''交通考试'''
@classmethod
def setUpClass(self):
self.driver=webdriver.Firefox()
self.driver.get('/')
def test_answer(self):
self.driver.find_element_by_xpath("/html/body/div[2]/div[4]/div/dl/dd[2]/div/div/div/table/tbody/tr/td/table/tbody/tr/td/input").click()
for i in range(2,101):
i=str(i)
path="/html/body/div[2]/div[4]/div/dl/dd[2]/div/div["+i+"]/div/table/tbody/tr/td/table/tbody/tr/td/input"
try:
self.driver.find_element_by_xpath(path).click()
except NoSuchElementException:
path="/html/body/div[2]/div[4]/div/dl/dd[2]/div/div["+i+"]/table/tbody/tr/td/table/tbody/tr/td/input"
self.driver.find_element_by_xpath(path).click()
self.driver.find_element_by_xpath("/html/body/div[2]/div[4]/div/dl/dd[2]/div/div[101]/span/input").click()
target=u'你的得分是'
text=self.driver.find_element_by_xpath("/html/body/div/div[4]/div[1]/dl[1]/dd[2]/div/div[101]/b").text
#print text
self.assertIn(target,text)
@classmethod
def tearDownClass(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
这是最后结果,可以看到一顺的A往下选,连击100下,还是很舒畅的~
转载于:.html
selenium,unittest——驾照科目一网上答题自动化
需求很简单,所有题目全选A,然后点提交出分,校验是否到达出分这步
遇到的坑有这几个,一个是assertIn哪个是校验哪个是文本要分清,还有code的编码统一到Unicode,最后就是xpath定位各个题目的A的路径还不是完全一样,需要catch有问题的进行调整
#encoding=utf-8
from selenium import webdriver
import time,unittest
from seleniummon.exceptions import NoSuchElementException
'''
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#此方法是将Python2的默认编码ASCII改为 utf-8
'''
class Testwenquan(unittest.TestCase):
'''交通考试'''
@classmethod
def setUpClass(self):
self.driver=webdriver.Firefox()
self.driver.get('/')
def test_answer(self):
self.driver.find_element_by_xpath("/html/body/div[2]/div[4]/div/dl/dd[2]/div/div/div/table/tbody/tr/td/table/tbody/tr/td/input").click()
for i in range(2,101):
i=str(i)
path="/html/body/div[2]/div[4]/div/dl/dd[2]/div/div["+i+"]/div/table/tbody/tr/td/table/tbody/tr/td/input"
try:
self.driver.find_element_by_xpath(path).click()
except NoSuchElementException:
path="/html/body/div[2]/div[4]/div/dl/dd[2]/div/div["+i+"]/table/tbody/tr/td/table/tbody/tr/td/input"
self.driver.find_element_by_xpath(path).click()
self.driver.find_element_by_xpath("/html/body/div[2]/div[4]/div/dl/dd[2]/div/div[101]/span/input").click()
target=u'你的得分是'
text=self.driver.find_element_by_xpath("/html/body/div/div[4]/div[1]/dl[1]/dd[2]/div/div[101]/b").text
#print text
self.assertIn(target,text)
@classmethod
def tearDownClass(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
这是最后结果,可以看到一顺的A往下选,连击100下,还是很舒畅的~
转载于:.html