【Python Selenium】简单数据生成脚本

2018-06-18 01:46:05来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

最近因工作需要,写了一个简单的自动化脚本,纯属学习,顺便学习下selenium模块。

废话不多说,直接上代码!!

这里一位大神重写了元素定位、send_keys等方法,咱们直接进行调用。

适用Python3.6

  1 # coding=utf-8
  2 
  3 """
  4 basePageUtil.py
  5 基础类basePage,封装所有页面都公用的方法,
  6 定义open函数,重定义find_element,switch_frame,send_keys等函数。
  7 在初始化方法中定义驱动driver,url,pagetitle
  8 """
  9 
 10 from selenium.webdriver.support.wait import WebDriverWait
 11 from selenium.webdriver.support import expected_conditions as EC
 12 from selenium.webdriver import ActionChains
 13 
 14 
 15 class basePage(object):
 16     """
 17     basePage封装所有页面都公用的方法,例如driver, url ,FindElement等
 18     """
 19     # 初始化driver,url,pagetitle等
 20     # 实例化basePage类时,最先执行的就是__init__方法,该方法的入参,其实就是basePage类的入参。
 21     #__init__方法不能有返回值,只能返回None
 22     # self只实例本身,相较于类Page而言。
 23 
 24     def __init__(self, selenium_driver, url, pagetitle='',downloadir='',pageurl=''):
 25         self.driver = selenium_driver
 26         self.url = url
 27         self.pagetitle = pagetitle
 28         self.downloadir = downloadir
 29         self.pageurl=pageurl
 30 
 31     # 通过title断言进入的页面是否正确。
 32     # 使用title获取当前窗口title,检查输入的title是否在当前title中,返回比较结果(True 或 False)
 33     def on_page(self, pagetitle):
 34         return pagetitle in self.driver.title
 35 
 36     # 打开页面,并校验页面链接是否加载正确
 37     # 以单下划线_开头的方法,在使用import *时,该方法不会被导入,保证该方法为类私有的。
 38     def _open(self, url, pagetitle='',pageurl=''):
 39         # 使用get打开访问链接地址
 40         self.driver.get(url)
 41         self.driver.maximize_window()
 42         print(self.driver.title,self.driver.current_url)
 43         if pagetitle:
 44             # 使用assert进行校验,打开的窗口title是否与配置的title一致。调用on_page()方法
 45             assert self.on_page(pagetitle), "Check Page Error:\t%s" % url
 46         if pageurl:
 47             # 校验打开后的url与传入url是否一致
 48             assert pageurl==self.driver.current_url,'{0}!={1}'.format(pageurl,self.driver.current_url)
 49 
 50     # 定义open方法,调用_open()进行打开链接
 51     def open(self):
 52         self._open(self.url, self.pagetitle,self.pageurl)
 53 
 54     # 重写元素定位方法
 55     def find_element(self, loc):
 56         try:
 57             # 等待元素可见
 58             return WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located(loc))
 59         except Exception as e:
 60             elements = WebDriverWait(self.driver, 5).until(EC.visibility_of_any_elements_located(loc))
 61             return elements[0] if elements else False
 62 
 63     #重写元素定位方法
 64     def find_elements(self, loc):
 65         try:
 66             # 等待元素可见
 67             return WebDriverWait(self.driver, 5).until(EC.visibility_of_any_elements_located(loc))
 68         except BaseException:
 69             print('page {0} does not have locator {1}'.format(self, loc))
 70 
 71     # 重写switch_frame方法
 72     def switch_frame(self, loc):
 73         return self.driver.switch_to_frame(loc)
 74 
 75     # 重写switch_frame方法
 76     def switch_window(self, loc):
 77         return self.driver.switch_to_window(loc)
 78 
 79     # 定义script方法,用于执行js脚本
 80     def script(self, src):
 81         self.driver.execute_script(src)
 82 
 83     # 重写定义send_keys方法
 84     def send_keys(self, loc: object, vaule: object, clear_first: object = True, click_first: object = True) -> object:
 85         try:
 86             if click_first:
 87                 self.find_element(loc).click()
 88             if clear_first:
 89                 self.find_element(loc).clear()
 90 
 91             self.find_element(loc).send_keys(vaule)
 92         except AttributeError:
 93             print('%s page does not have "%s" locator' % (self, loc))
 94 
 95     # 重写鼠标悬停方法
 96     def move_to_element(self, element='', loc=''):
 97         if loc:
 98             element = self.find_element(loc)
 99         elif not element:
100             assert False,'Not Found Element'
101         ActionChains(self.driver).move_to_element(element).perform()

自己写的脚本如下,脚本有些简单,没有进行类的封装(暂时还不会,慢慢学习):

"""
selenium驱动Firefox实现人工操作进行数据生成,输出日志到文本文件
"""
from basePageUtil import *
from selenium import webdriver
from selenium.webdriver.common.by import By
import xlrd
import re
import time
from datetime import datetime,timedelta
from selenium.webdriver.common.keys import Keys

#读取Excel表格数据
def excle_table(file,table,colnameindex,file_log,fb,by_index=0):
    # 某一行数据
    colvalues = table.row_values(colnameindex)
    #定义一个空列表,用来存放Excel的每行数据
    # list = []
    #表头数据
    row = table.row_values(0)
    if row:
        list = {}
        for i in range(len(colvalues)):
            if i ==10:
                try:
                    #格式化Excel表日期
                    excel_date = xlrd.xldate.xldate_as_datetime(colvalues[i], 0)
                    #日期格式化为str类型
                    dt = datetime.strptime(str(excel_date), "%Y-%m-%d %H:%M:%S").strftime('%Y-%m-%d')
                    #将日期数据传入到相应的表头下
                    list[row[i]] = dt
                except Exception:
                    # e_bd = '第{0}条数据,初登日期格式错误'.format(colnameindex) + '\n'
                    # fb.write(e_bd)
                    continue
            elif i == 11 or i ==12:
                try:
                    # 格式化Excel表日期
                    excel_date = xlrd.xldate.xldate_as_datetime(colvalues[i], 0)
                    # 日期格式化为datetime类型
                    dt = datetime.strptime(str(excel_date), "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %H:%M")
                    # 将日期数据传入到相应的表头下
                    list[row[i]] = dt
                except Exception:
                    # e_edate = '第{0}条数据,起保日期、终保日期格式错误'.format(colnameindex) + '\n'
                    # fb.write(e_edate)
                    continue
            else:
                list[row[i]] = colvalues[i]
    return list
def flag(year):
    if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
        return True
    else:
        return False

def login():
    driver = webdriver.Firefox()
    url = 'http://172.20.xx.xx/xxxx/login.do'
    driver.get(url)
    driver.maximize_window()
    UserName = "username"
    PassWord = "password"

    RawData = basePage(driver, url)
    # 登录
    RawData.find_element((By.ID, 'j_username')).send_keys(UserName)
    RawData.find_element((By.ID, 'j_password')).send_keys(PassWord)
    RawData.find_element((By.ID, 'login_ok')).click()
    # 点击数据生成
    RawData.find_element((By.LINK_TEXT, '数据生成')).click()

    # 点击商业险数据生成
    RawData.find_element((By.ID, 'main_treeDemo_34_9_span')).click()
    #数据文件路径
    file = r"C:\Users\zg\Desktop\data.xlsx"
    # 打开excle文件
    data = xlrd.open_workbook(file)
    # 读取第一个sheet页
    table = data.sheet_by_index(0)
    # 行数
    nrows = table.nrows
    #错误日志文件
    file_out = r'C:\Users\zg\Desktop\log.txt'
    DateNow = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    fb = open(file_out,'a')
    fb.write(DateNow + '\n')
    # 数据条数
    succ_num = 0
    fail_num = 0
    NUM = nrows
    for data_index in range(1,NUM):

        listdata = excle_table(file,table,data_index,file_out,fb)
        if (len(listdata) <= 0):
            assert 0, u'Excle数据异常'
        try:
            #省份选择
            RawData.find_element((By.XPATH,'//a[@lookupgroup=\'cityLookup\']/i')).click()
            # RawData.find_element((By.LINK_TEXT, '河北')).click()
            province = listdata['省份']
            time.sleep(0.5)
            #js设置搜索框大小
            scri1 = "var doc = document.getElementById('city');doc.style.height='685px';"
            scri2 = "var doc = document.getElementsByTagName('div');" \
                    "for(var i=0;i < doc.length;i++){" \
                    " if (doc[i].className == 'dialog dialog-box'){" \
                    "doc[i].style.top='20px';}}"
            scri3 = "var doc = document.getElementsByTagName('div');" \
                    "for(var i=0;i < doc.length;i++){" \
                    " if (doc[i].className == 'dialogContent layoutBox unitBox'){" \
                    "doc[i].style.height='690px';}}"
            #执行js脚本
            driver.execute_script(scri1)
            driver.execute_script(scri2)
            driver.execute_script(scri3)
            try:
                RawData.find_element((By.LINK_TEXT,province)).click()
            except:
                e_pro = '第{0}条数据省份有误,'.format(data_index)
                fb.write(e_pro)
                raise

            #公司名称
            RawData.find_element((By.XPATH,'//a[@id=\'companySelect\']/i')).click()
            #获取公司名称
            company = re.search('[^a-zA-Z\s]+', listdata['需要生成哪家公司的数据']).group()
            try:
                # 输入公司名称
                RawData.find_element((By.ID, 'compname')).send_keys(company)
                #点击检索
                RawData.find_element((By.XPATH, "//button[text()='检索']")).click()
                #点击选择
                RawData.find_element((By.LINK_TEXT, '选择')).click()
            except:
                RawData.find_element((By.XPATH, '/html/body/div[12]/div[1]/div/div/a[1]/i')).click()
                e_ComCode = '第{0}条数据公司名称有误,'.format(data_index)
                fb.write(e_ComCode)
                raise
            try:
                #数据库选择
                RawData.find_element((By.XPATH,"//button[@data-id='commerAreaVsUrl']")).click()
                loc_db = "/html/body/div/div/ul/li/a/span[text()='{0} 联调']".format(province)
                RawData.find_element((By.XPATH, loc_db)).click()
            except:
                e_db = '第{0}条数据数据库名称有误,'.format(data_index)
                fb.write(e_db)
                raise

            try:
                #业务种类选择
                RawData.find_element((By.XPATH,"//button[@data-id='biz_bjautotypes']")).click()
                YWZL = listdata['业务种类']
                RawData.find_element((By.XPATH, "/html/body/div[13]/div/ul/li/a/span[text()='" + YWZL + "']")).click()
            except:
                e_YWZL = '第{0}条数据业务种类有误,'.format(data_index)
                fb.write(e_YWZL)
                raise
            # 日期
            # 起保日期
            eds = driver.find_elements_by_xpath("//input[@name='effectiveDate']")
            # 终保日期
            exds = driver.find_elements_by_xpath("//input[@name='expireDate']")
            # 签单日期
            bds = driver.find_elements_by_xpath("//input[@name='billDate']")
            # 投保查询日期
            qds = driver.find_elements_by_xpath("//input[@name='queryDate']")
            # 起保日期
            effdt_data = listdata['起保日期']
            # 终保日期
            exdt_data = listdata['终保日期']
            # 起保日期格式化
            effdt_data_tra = datetime.strptime(effdt_data, "%Y-%m-%d %H:%M")
            # 终保日期格式化
            exdt_data_tra = datetime.strptime(exdt_data, "%Y-%m-%d %H:%M")
            #业务种类列表
            YWZL1 = ['连续5年没有发生赔款','连续4年没有发生赔款','连续3年没有发生赔款','连续2年没有发生赔款','连续1年没有发生赔款']
            years = re.search('\d', YWZL).group()
            #对业务种类进行判断
            if YWZL in YWZL1:

                if flag(effdt_data_tra.year) == True and effdt_data_tra.month == 2 and effdt_data_tra.day == 29:
                    flagYear = True
                else:
                    flagYear = False
                for i in range(0, int(years)):
                    # 传入日期前数据清空
                    eds[i].clear()
                    exds[i].clear()
                    bds[i].clear()
                    qds[i].clear()
                    # 起保日期年份
                    effdt_year = effdt_data_tra.year
                    #起保日期减一天
                    effdt_now = effdt_data_tra - timedelta(days=1)
                    # 传入起保日期
                    eds[i].send_keys(str(effdt_data_tra.strftime("%Y-%m-%d %H:%M")))
                    # 传入终保日期
                    exds[i].send_keys(str(exdt_data_tra.strftime("%Y-%m-%d %H:%M")))
                    # 传入签单日期
                    bds[i].send_keys(effdt_now.strftime("%Y-%m-%d"))
                    # 传入投保查询日期
                    qds[i].send_keys(effdt_now.strftime("%Y-%m-%d"))
                    # 终保日期
                    exdt_data_tra = effdt_data_tra
                    if flagYear == True:
                        if flag(effdt_year - 1) == True:
                            effdt_data_tra_str = "{0}-02-29 00:00".format(effdt_year - 1)
                        if flag(effdt_year - 1) == False:
                            effdt_data_tra_str = "{0}-02-28 00:00".format(effdt_year - 1)
                        effdt_data_tra = datetime.strptime(effdt_data_tra_str, "%Y-%m-%d %H:%M")
                    else:
                        if flag(effdt_year) == True and (
                                effdt_data_tra.month > 2 or effdt_data_tra.month == 2 and effdt_data_tra.day == 29):
                            # 起保日期减366天
                            effdt_data_tra = effdt_data_tra - timedelta(days=366)
                        elif flag(effdt_year) == False and flag(effdt_year - 1) == True and effdt_data_tra.month < 3:
                            # 起保日期减366天
                            effdt_data_tra = effdt_data_tra - timedelta(days=366)
                        else:
                            # 起保日期减365天
                            effdt_data_tra = effdt_data_tra - timedelta(days=365)
            else:
                # 结案时间
                CTs = RawData.find_elements((By.XPATH, "//input[@name='ClaimCloseTime']"))

                for i in range(0,int(years)):
                    #传入日期前数据清空
                    eds[0].clear()
                    exds[0].clear()
                    bds[0].clear()
                    qds[0].clear()
                    CTs[i].clear()
                    # 起保日期年份
                    effdt_year = effdt_data_tra.year
                    # 起保日期减一天
                    effdt_now = effdt_data_tra - timedelta(days=1)
                    # 起保日期加一天,结案时间
                    CT_now = effdt_data_tra + timedelta(days=i+1)
                    # 传入起保日期
                    eds[0].send_keys(str(effdt_data_tra))
                    # 传入终保日期
                    exds[0].send_keys(str(exdt_data_tra))
                    # 传入签单日期
                    bds[0].send_keys(effdt_now.strftime("%Y-%m-%d"))
                    # 传入投保查询日期
                    qds[0].send_keys(effdt_now.strftime("%Y-%m-%d"))
                    # 传入结案时间
                    CTs[i].send_keys(CT_now.strftime("%Y-%m-%d %H:%M"))
            #向上翻页
            RawData.find_element((By.ID, 'bjautolayout_table_css')).send_keys(Keys.PAGE_UP)
            try:
                #号牌选择
                RawData.find_element((By.XPATH,"//button[@data-id='autolicenseType']")).click()
                license = listdata['号牌种类'].strip()
                RawData.find_element((By.XPATH, "/html/body/div[14]/div/ul/li/a/span[text()='"+license+"']")).click()
            except:
                e_license = '第{0}条数据号牌种类有误,'.format(data_index)
                fb.write(e_license)
                raise

            try:
                #车辆类型
                RawData.find_element((By.XPATH,"//button[@data-id='car_type']")).click()
                CLLX = listdata['车辆种类']
                RawData.find_element((By.XPATH, "/html/body/div[15]/div/ul/li/a/span[text()='"+ CLLX +"']")).click()
            except:
                e_CLLX = '第{0}条数据车辆类型有误,'.format(data_index)
                fb.write(e_CLLX)
                raise

            try:
                #使用性质
                RawData.find_element((By.XPATH,"//button[@data-id='car_type2s']")).click()
                SYXZ = listdata['使用性质'].strip()
                RawData.find_element((By.XPATH, "/html/body/div[16]/div/ul/li/a/span[text()='"+ SYXZ +"']")).click()
            except:
                e_SYXZ = '第{0}条数据使用性质有误,'.format(data_index)
                fb.write(e_SYXZ)
                raise

            #车主姓名
            # RawData.find_element((By.ID,"roleName")).send_keys('张三')

            #保额
            # RawData.find_element((By.ID,"sumLimit")).send_keys('1000')

            try:
                #车三项
                RawData.find_element((By.XPATH,"//button[@data-id='iscarform']")).click()
                RawData.find_element((By.XPATH,"/html/body/div[17]/div/ul/li/a/span[text()='否']")).click()
            except:
                e_CSX = '第{0}条数据车三项选择有误,'.format(data_index)
                fb.write(e_CSX)
                raise

            try:
                #初登日期
                Registerdate = str(listdata['初登日期'])
                RawData.find_element((By.ID, "registerdate")).send_keys(Registerdate)
            except:
                e_registerdate = '第{0}条数据初登日期有误,'.format(data_index)
                fb.write(e_registerdate)
                raise

            try:
                #是否过户车
                RawData.find_element((By.XPATH,"//button[@data-id='transferid']")).click()
                GHC = listdata['是否过户车'].strip()
                RawData.find_element((By.XPATH,"/html/body/div[18]/div/ul/li/a/span[text()='"+GHC+"']")).click()
            except:
                e_GHC = '第{0}条数据过户车选择有误,'.format(data_index)
                fb.write(e_GHC)
                raise

            try:
                #数据条数
                DataNum = str(int(listdata['数据条数']))
                # DataNum = re.search('\d+',listdata['数据条数']).group()
                RawData.find_element((By.ID, "commertshu")).send_keys(DataNum)
            except:
                e_DataNum = '第{0}条数据数据条数有误,'.format(data_index)
                fb.write(e_DataNum)
                raise

            try:
                #是否是异地车
                RawData.find_element((By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/form/div[1]/table/tbody/tr[39]/td[2]/div/button")).click()
                YDC = listdata['是否异地车'].strip()
                RawData.find_element((By.XPATH,"/html/body/div[19]/div/ul/li/a/span[text()='"+YDC+"']")).click()
            except:
                e_YDC = '第{0}条数据异地车选择有误,'.format(data_index)
                fb.write(e_YDC)
                raise

            # 保单系数算法
            RawData.find_element((By.XPATH,"//button[@data-id='case_type2']")).click()
            RawData.find_element((By.XPATH,"/html/body/div[20]/div/ul/li/a/span[text()='新']")).click()

            # 多年车贷
            RawData.find_element((By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/form/div[1]/table/tbody/tr[40]/td[4]/div/button")).click()
            RawData.find_element((By.XPATH,"/html/body/div[21]/div/ul/li/a/span[text()='否']")).click()


            try:
                #提交公司代码
                RawData.find_element((By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/form/div[1]/table/tbody/tr[41]/td[2]/div/button")).click()
                ComCode = listdata['本公司名称'].replace(' ','-').strip()
                RawData.find_element((By.XPATH, "/html/body/div[22]/div/ul/li/a/span[text()='"+ ComCode +"']")).click()
            except:
                e_ComCode = '第{0}条数据公司代码有误,'.format(data_index)
                fb.write(e_ComCode)
                raise
            #点击提交
            RawData.find_element((By.XPATH,"//button[text()='提交']")).click()
            # time.sleep(3)
            RawData.find_element((By.XPATH,"//button[text()='确定']")).click()
            #刷新商业数据生成页面
            RawData.find_element((By.ID, 'main_treeDemo_34_9_span')).click()
            # 成功条数d
            succ_num += 1
            time.sleep(0.5)
            e_succ = '第{0}条数据生成成功'.format(data_index) + '\n'
            fb.write(e_succ)
        except:
            e_fail = '数据生成失败,请校验数据。\n’
            fb.write(e_fail)
            # 刷新商业数据生成页面
            RawData.find_element((By.ID, 'main_treeDemo_34_9_span')).click()
            fail_num +=1
            time.sleep(0.5)
    fb.write('\n本次数据条数共{0}条,成功{1}条,失败{2}条。\n'.format(NUM - 1, succ_num, fail_num))
    fb.close()
if __name__== '__main__':
    login()

  代码比较简单,不喜勿喷,本人正在学习阶段!

  Python学习交流QQ群:25452556,欢迎大家加入(群主就是重写代码那位)!

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python输入输出

下一篇:python IDLE中反斜杠显示为人民币符号¥的解决办法