用Python+Selenium获取XX省交通违章数据

发表于:2016-9-02 11:29

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Python_博客园    来源:51Testing软件测试网采编

  前言:
  目前在研究易信公众号,想给公众号增加一个获取个人交通违章的查询菜单,通过点击返回查询数据。以下是实施过程。
  一、首先,用火狐浏览器打开 XX省交管网 ,分析页面信息:
  可以看到共有4种查询种类,我只要查询违章数据,所以分析第一个 电子警察信息查询 就好了,用firebug分别查看车牌号码、车辆识别码、验证码输入框,可以得到id属性,分别为:carNum1、carAuthCode1、captcha1。
  到这里,我们可以用selenium根据获取的id,自动填入车牌号码、车辆识别码、验证码,但验证码如何获取呢?。
  二、获取验证码
  第一次、通过Tesseract识别
  经过测试,识别率太低了,不可行。
  第二次、通过cookies查找验证码
  通过查看服务器返回的cookies,发现里面竟然有验证码。。。
  三、编写程序测试
  1、流程图和测试结果
  2、源代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class JTWZ():
def __init__(self,carAuthCode,carNum):
"""
carAuthCode:车辆识别码
carNum:车牌号
"""
self.driver = webdriver.Chrome()
self.url = 'http://xxcx.hbsjg.gov.cn:8087/hbjj/'
self.carAuthCode=carAuthCode
self.carNum=carNum
def get_content(self):
self.driver.get(self.url)
try:
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "checkCode1")))
print(u'开始登录...')
except Exception as e:
print(e)
self.carNum1 = self.driver.find_element_by_id('carNum1')
self.carNum1.send_keys(self.carNum)
self.carAuthCode1 = self.driver.find_element_by_id('carAuthCode1')
self.carAuthCode1.send_keys(self.carAuthCode)
captcha1=self.driver.find_element_by_id('captcha1')
#从cookies找寻验证码
for n in self.driver.get_cookies():
if n.get('name')!=None and n['name']=='RANDOMVALIDATECODEKEY1':
checkCode1=n['value']
captcha1.send_keys(checkCode1)
sub=self.driver.find_element_by_xpath("//input[@value='开始查询']")
sub.click()
try:
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "fsmiddle")))
print(u'获取违章内容成功,保存为:wz.jpg...')
self.driver.save_screenshot('wz.jpg')
return 0
except:
print(u'获取失败...')
return 1
finally:
self.driver.quit()
if __name__ == '__main__':
jtwz=JTWZ(carAuthCode=000,carNum='')
jtwz.get_content()
View Code
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号