一个简单的appium以及selenium相结合调用来测APP的例子

# ! /usr/bin/env python
# coding=utf-8
from appium import webdriver
import re
import pymysql
import time
import shutil,os
from PIL import Image
import pytesseract
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import bmethod

def get_mobile():
    with open('mobile.yml', 'r') as f:
        mobile=f.read()
        print (mobile)
    new_mobile = int(mobile) + 1
    with open('mobile.yml', 'w') as f:
        f.write(str(new_mobile))
    return mobile
mobile=get_mobile()

def get_nric_no():
    with open('nric_no.yml', 'r') as f:
        nric_no=f.read()
        print (nric_no)
    new_nrci = int(nric_no) + 1
    with open('nric_no.yml', 'w') as f:
        f.write(str(new_nrci))
    return nric_no
nric_no=get_nric_no()



def IDcard_confirm(mobile, nric_no):
    print(mobile,nric_no)
    # 打开数据库连接
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan", port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    # 使用 execute()  方法执行 SQL 查询customer_id
    cursor.execute("SELECT id FROM customer WHERE mobile=%s;" % mobile)

    # 使用 fetchone() 方法获取单条数据.
    cus_id = cursor.fetchone()
    # 更新身份证信息
    updateID = """UPDATE `customer`
SET 
 `nric_no` = '%s',
 `nric_front_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151913_1617439745.jpg',
 `nric_back_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151905_1457736902.jpg',
 `fullface_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151856_138829020.jpg'
WHERE
    (`mobile` = '%s');""" % (nric_no, mobile)
    cursor.execute(updateID)
    db.commit()
    ###插入身份证认证状态
    update_status = """INSERT INTO `vn-loan`.`customer_certify_info` (
    `customer_id`,
    `credit_source_id`,
    `credit_source_ver`,
    `certify_type`,
    `certify_result`,
    `must_passed`,
    `failure_reason`,
    `failure_code`,
    `report`,
    `report_status`,
    `repull_times`,
    `expired_at`,
    `created_at`,
    `updated_at`
)
VALUES
    (

        '%s',
        '128',
        '',
        '10',
        '2',
        '1',
        NULL,
        NULL,
        '',
        '0',
        '0',
        '2019-09-10 15:50:19',
        '2019-03-14 15:51:51',
        '2019-03-14 15:50:19'
    );
""" % cus_id
    cursor.execute(update_status)
    db.commit()
    # 关闭数据库连接
    db.close()


def get_CAPTCHA(box):

    im = Image.open("image.jpg")
    imbox=im.crop(box)
    imbox.save('image1.gif')
    image = Image.open('image1.gif')
    vcode = pytesseract.image_to_string(image)
    print(vcode)

    return vcode

def findItem(el):
    source = driver.page_source
    if el in source:
        return 0
    else:
        return 1

def always_allow(driver, number=5):
    '''
    fuction:权限弹窗-始终允许
    args:1.传driver
    2.number,判断弹窗次数,默认给5次
    其它:
    WebDriverWait里面0.5s判断一次是否有弹窗,1s超时
    '''
    for i in range(number):
        loc = ("xpath", "//*[@text='始终允许']")
        loc1 = ("xpath", "//*[@text='允许']")
        try:
            e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
            e.click()
            r = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc1))
            r.click()
        except:
            pass

def google_confim(driver, number=3):
    '''
    fuction:权限弹窗-始终允许
    args:1.传driver
    2.number,判断弹窗次数,默认给5次
    其它:
    WebDriverWait里面0.5s判断一次是否有弹窗,1s超时
    '''
    for i in range(number):
        loc = ("xpath", "//*[@text='确定']")
        try:
            e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
            e.click()
        except:
            pass

def update_cancel(driver):
    loc = ("id", "com.xdong.xdong:id/cancel")
    try:
        e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
        e.click()
    except:
        pass

######################################################################################################################################
mobilelist={'1':'夜神','2':'三星','3':'华为','4':'oppo'}
machine={'1':'127.0.0.1:62001','2':'三星','3':'华为','4':'127.0.0.1:5307'}
appname={'1':'MRdong','2':'Wocash','3':'xdong'}
appPackages={'1':'com.dongnan.mrdong','2':'com.dongnan.wocash','3':'com.xdong.xdong'}
appActivitys={'1':'com.mrdong.mrdong.MainActivity','2':'com.wocash.wocash.MainActivity','3':'com.xdong.xdong.MainActivity'}

print('请选择您需要使用的手机:1.夜神  2.三星  3.华为')
# a = input("请输入相应数字:")
a=1
b=3
os.system(r'call C:\progra~2\Nox\bin\nox_adb.exe connect %s'%machine[str(a)])

mobiletype=mobilelist[str(a)]
app=appname[str(b)]
print('即将为您使用%s.%s手机来做自动化测试%s'%(a,mobiletype,app))
appPackage=appPackages[str(b)]
appActivity=appActivitys[str(b)]

############################手机模式#######################################
if mobiletype=='夜神':
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1.1'
    desired_caps['deviceName'] = '127.0.0.1:62001'
    desired_caps['appPackage'] = appPackage
    desired_caps['appActivity'] = appActivity
    desired_caps['newCommandTimeout']='600'
    print(desired_caps)
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    chromed='chromedriver-213.exe'


elif mobiletype=='三星':
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.0.0'
    desired_caps['deviceName'] = '42008a609b6d5479'
    desired_caps['appPackage'] = appPackage
    desired_caps['appActivity'] = appActivity
    print(desired_caps)
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    chromed = 'chromedriver-245.exe'


elif mobiletype=='华为':
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.0.0'
    desired_caps['deviceName'] = 'TPE9X18614W05318'
    desired_caps['appPackage'] = appPackage
    desired_caps['appActivity'] = appActivity
    print(desired_caps)
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    chromed = 'chromedriver-238.exe'

elif mobiletype=='oppo':
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.1.0'
    desired_caps['deviceName'] = 'aa41aa3'
    desired_caps['appPackage'] = appPackage
    desired_caps['appActivity'] = appActivity
    print(desired_caps)
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    chromed = 'chromedriver-238.exe'

############################替换chromedriver#######################################
path_driver=r'C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\\'
if os.path.exists(path_driver+"chromedriver.exe"):
    os.unlink(path_driver+"chromedriver.exe")
shutil.copy(path_driver+chromed,path_driver+"chromedriver.exe")
time.sleep(1)

# #############################登录##############################
# mobile=2334569088
# driver.implicitly_wait(3)
# google_confim(driver,number=3)
# time.sleep(1)
# print(driver.contexts)
# driver.switch_to.context(driver.contexts[1])
# driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/input').send_keys(mobile)
# driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/input').send_keys('123456')
# driver.find_element_by_xpath('//*[@id="app"]/div/button').click()
# time.sleep(3)

############################注册#######################################

driver.implicitly_wait(4)
google_confim(driver,number=1)
time.sleep(1)
always_allow(driver,number=1)
time.sleep(2)
update_cancel(driver)
print(driver.contexts)
time.sleep(3)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_element_by_class_name('register-button').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/input').send_keys(mobile)
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/div/div').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div/div[7]/div[2]/div[2]/div/img').click()
driver.switch_to.context(driver.contexts[0])
time.sleep(2)
driver.get_screenshot_as_file(os.getcwd() + '\\image.jpg')
time.sleep(2)
box=(674,945,922,1061)
CAPTCHA=get_CAPTCHA(box)
driver.switch_to.context(driver.contexts[1])
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div/div[7]/div[2]/div[2]/div/input').send_keys(CAPTCHA)
driver.find_element_by_class_name('ok').click()
time.sleep(2)
while 1:
    ele_cap='/api/auth/captcha'
    if findItem(ele_cap) == 0:
        driver.find_element_by_xpath('//*[@id="app"]/div/div[7]/div[2]/div[2]/div/img').click()
        driver.switch_to.context(driver.contexts[0])
        time.sleep(3)
        driver.get_screenshot_as_file(os.getcwd() + '\\image.jpg')
        time.sleep(2)
        CAPTCHA = get_CAPTCHA(box)
        driver.switch_to.context(driver.contexts[1])
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/div/div[7]/div[2]/div[2]/div/input').clear()
        driver.find_element_by_xpath('//*[@id="app"]/div/div[7]/div[2]/div[2]/div/input').send_keys(CAPTCHA)
        driver.find_element_by_class_name('ok').click()
        time.sleep(2)
    else:
         break
time.sleep(2)
code=bmethod.get_code(mobile)
driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/input').send_keys(code)
driver.find_element_by_xpath('//*[@id="app"]/div/div[5]/input').send_keys('123456')
driver.find_element_by_class_name('register-btn').click()
time.sleep(3)

############################认证#######################################

driver.find_element_by_class_name('confirm-button').click()
driver.find_element_by_class_name('ok').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[1]/div[2]/input').send_keys('kenny')
driver.find_elements_by_class_name('select-button')[0].click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/span[2]').click()
time.sleep(1)
driver.find_elements_by_class_name('select-button')[1].click()
time.sleep(1)
driver.switch_to.context(driver.contexts[0])
driver.swipe(520,1850,370,800)
time.sleep(1)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_element_by_class_name('picker-confirm').click()
time.sleep(1)
driver.find_elements_by_class_name('select-button')[2].click()
driver.switch_to.context(driver.contexts[0])
driver.swipe(520,1850,370,800)
time.sleep(1)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_element_by_class_name('picker-confirm').click()
time.sleep(1)
driver.find_elements_by_class_name('select-button')[3].click()
driver.switch_to.context(driver.contexts[0])
driver.swipe(520,1850,370,1700)
time.sleep(1)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_element_by_class_name('picker-confirm').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[5]/div[2]/input[1]').send_keys('huzhiming')
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[5]/div[2]/input[2]').send_keys('hainei')
# driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[6]/div[2]/input').send_keys('https://www.facebook.com/kkkkk')
driver.find_element_by_class_name('confirm-button').click()
time.sleep(1)
driver.find_element_by_class_name('ok').click()
time.sleep(2)
driver.find_element_by_class_name('back').click()
###########由于没有摄像头,直接选择数据库认证身份证
IDcard_confirm(mobile,nric_no)
print('身份证认证成功!')
driver.refresh()
time.sleep(2)
###########联系人认证
driver.find_element_by_class_name('confirm-button').click()
driver.find_element_by_class_name('ok').click()
time.sleep(2)
driver.find_elements_by_class_name('contacts-certify-form-input')[0].click()
time.sleep(2)
driver.switch_to.context(driver.contexts[0])
driver.tap([(268,209),(1016,252)],100)
time.sleep(2)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_elements_by_class_name('radio-core')[0].click()
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[4]/div[2]/input').send_keys('Daddy')
time.sleep(2)
driver.find_elements_by_class_name('contacts-certify-form-input')[1].click()
time.sleep(2)
driver.switch_to.context(driver.contexts[0])
driver.tap([(268,337),(1016,380)],100)
time.sleep(2)
driver.swipe(520,1850,370,800)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_elements_by_class_name('radio-core')[3].click()
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[4]/div[2]/input').send_keys('Mami')
driver.find_element_by_class_name('confirm-button').click()
time.sleep(4)
###################确认借款
driver.find_element_by_class_name('confirm-button').click()
time.sleep(3)

################################审批订单########
bmethod.change_auditor(mobile)
time.sleep(2)
bmethod.audit_order()
time.sleep(2)
######################刷新状态并添加银行卡############
driver.refresh()
time.sleep(2)
driver.find_element_by_class_name('confirm-select-bank').click()
time.sleep(1)
driver.find_element_by_class_name('add-more-card').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[2]/input').send_keys('058888888')
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[3]/input').send_keys('H')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[3]/input').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[3]/div/ul/li[1]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[4]/input').send_keys('e')
time.sleep(2)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[4]/input').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[4]/div/ul/li[6]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[5]/input').send_keys('e')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[5]/input').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[5]/div/ul/li[5]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/ul/li[6]/input').send_keys(mobile)
time.sleep(1)
driver.find_element_by_class_name('confirm-button').click()
time.sleep(1)
driver.find_element_by_class_name('ok').click()
time.sleep(3)
driver.find_element_by_class_name('back').click()
time.sleep(2)
#############银行卡添加完成,申请订单############
driver.switch_to.context(driver.contexts[0])
driver.swipe(520,1850,370,1700)
time.sleep(1)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
driver.find_element_by_class_name('confirm-button').click()
time.sleep(3)
driver.find_element_by_class_name('ok').click()
time.sleep(2)

######################放款###########
bmethod.make_loan()

######################逾期###########

overdays_set=int(bmethod.overtime_loan(mobile))

driver.refresh()
time.sleep(4)
#################计算还款计划中逾期金额##########
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[1]/div[1]/div/div[1]/div[2]/span[2]').click()
time.sleep(2)
a=driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[3]/div[4]/div[2]').text
overdays=int(re.findall('\d+',a)[0])
b=driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div[2]/div[4]/span[2]').text
overfee=int(re.findall('\d+',b)[0])
c=driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div[2]/div[8]/span[2]').text
allfee=int(re.findall('\d+',c)[0])
print(overdays,overfee,allfee)
if overdays==overdays_set and overfee==overdays*20000 and allfee==1000000+overfee:
    print('逾期日期正确,计算的逾期费用为%s,总金额%s计算正确'%(overfee,allfee))
elif overfee!=overdays*20000:
    print('逾期费用计算不正确')
elif allfee!=1000000+overfee:
    print('总费用不正确')

##############################入催##############
data=bmethod.collection(mobile,overdays)
all_collection=int(data[0])
overdays_cui=int(data[1])
need_payment_cui=int(data[3])
overfee_cui=int(data[2])
print(all_collection,overdays_cui,need_payment_cui,overfee_cui)
print('入催数据计算对比中。。。。')
if overfee_cui==overdays_cui*20000 and need_payment_cui==1000000+overfee_cui and all_collection==1000000+overfee_cui:
    print('计算的逾期费用为%s,总金额%s计算正确'%(overfee_cui,need_payment_cui))
elif overfee_cui!=overdays_cui*20000:
    print('逾期费用计算不正确')
elif need_payment_cui!=1000000+overfee_cui and all_collection!=1000000+overfee_cui:
    print('总费用不正确')

driver.find_element_by_class_name('back').click()#返回
time.sleep(2)
#######################################在线部分还款########################

driver.find_element_by_class_name('confirm-button').click()#还款
time.sleep(2)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/input').send_keys(all_collection)
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div[2]/div/div[2]/div/div/div[2]/div[2]/div/div[1]').click()
time.sleep(1)
driver.switch_to.context(driver.contexts[0])
for i in range(1,8):
    driver.swipe(520,1850,520,1776)
    time.sleep(1)
driver.switch_to.context(driver.contexts[1])
time.sleep(1)
driver.find_element_by_class_name('picker-confirm').click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div[2]/div/div[2]/div/div/div[3]/div[2]/div/div[1]').click()
time.sleep(2)
driver.switch_to.context(driver.contexts[0])
driver.swipe(520,1850,520,1776)
time.sleep(1)
driver.switch_to.context(driver.contexts[1])
driver.find_element_by_class_name('picker-confirm').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div[2]/div/div[2]/div/div/div[4]/div[2]/input').send_keys('abc@abc.com')
time.sleep(1)
driver.find_element_by_class_name('confirm-button').click()
time.sleep(4)
Select(driver.find_element_by_name("escrow")).select_by_index(1)
driver.find_element_by_name('address').send_keys('Payment with ATM')
time.sleep(1)
driver.find_element_by_name('card_number').send_keys('9874563254178962')
time.sleep(1)
driver.find_element_by_name('card_fullname').send_keys('Nguyen Hue')
time.sleep(1)
driver.switch_to.context(driver.contexts[0])
time.sleep(3)
driver.get_screenshot_as_file(os.getcwd() + '\\image.jpg')
box1=(629,1139,807,1226)
CAPTCHA=get_CAPTCHA(box1)
driver.switch_to.context(driver.contexts[1])
time.sleep(2)
el_code=driver.find_element_by_xpath('/html/body/div/div[4]/form/div/div/div[1]/div[5]/label').text
driver.find_element_by_name('verify_image').send_keys(CAPTCHA)
driver.find_element_by_xpath('/html/body/div/div[4]/form/div/div/div[2]/div/div[1]/button').click()
time.sleep(1)
while 1:
    el_code='Mã bảo mật:'
    if findItem(el_code) == 0:
        driver.switch_to.context(driver.contexts[0])
        time.sleep(1)
        driver.get_screenshot_as_file(os.getcwd() + '\\image.jpg')
        time.sleep(1)
        box1=(629,1139,807,1226)
        CAPTCHA = get_CAPTCHA(box1)
        driver.switch_to.context(driver.contexts[1])
        time.sleep(1)
        driver.find_element_by_name('verify_image').clear()
        driver.find_element_by_name('verify_image').send_keys(CAPTCHA)
        driver.find_element_by_xpath('/html/body/div/div[4]/form/div/div/div[2]/div/div[1]/button').click()
        time.sleep(1)
    else:
         break
time.sleep(2)
driver.find_element_by_xpath('/html/body/div/div[4]/form/div/div/div/div[2]/div/input').send_keys('123456')
driver.find_element_by_xpath('/html/body/div/div[4]/form/div/div/div/div[3]/div/button').click()
time.sleep(2)
driver.find_element_by_class_name('confirm-button').click()
time.sleep(2)
bmethod.huankuan_job()
#############################申请展期###############
# bmethod.delay_order()
driver.refresh()

另外个是调用后台的时候调用的方法,需要调用到selenium,因为两个driver不一样,所以需要分开两个文件,用调用的方法去驱动:

#coding=utf-8
from selenium import webdriver
import time,os,re
import pymysql
from selenium.webdriver.support.select import Select
from pykeyboard import PyKeyboard
from pymouse import PyMouse
import datetime
import csv
import pandas

import random
#读取全局配置文件
# path_all=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+r"\environment\env.conf"
# cf = ConfigParser.ConfigParser()
# cf.read(path_all)
mobile=2334569041
path=os.getcwd()


houtai='http://admin.vietnam.zetafin.cn/login'
renwu='http://jobs.zetafin.cn/toLogin'
rucui='http://collect.vietnam.zetafin.cn/login'

def get_code(mobile):
    # 打开数据库连接
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan" ,port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    # 使用 execute()  方法执行 SQL 查询
    cursor.execute("SELECT content FROM `sms_log` WHERE dest_mobile=%s ORDER BY created_at DESC LIMIT 1;"% mobile)

    # 使用 fetchone() 方法获取单条数据.
    data = cursor.fetchone()
    code=re.findall('\d+',str(data))[0]

    print("code : %s " % code)

    # 关闭数据库连接
    db.close()
    return code

def change_auditor(mobile):
    # 打开数据库连接
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan", port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    sql="""UPDATE loan_order
SET 
auditor_id = '1498',
auditor_name = '周佳毅'
WHERE
customer_mobile = '%s';
"""%mobile
    # 使用 execute()  方法执行 SQL 查询
    cursor.execute(sql)
    db.commit()
    # 关闭数据库连接
    db.close()
    print('审批人已经更改为1498')



def IDcard_confirm(mobile, nric_no):
    print(mobile,nric_no)
    # 打开数据库连接
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan", port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    # 使用 execute()  方法执行 SQL 查询customer_id
    cursor.execute("SELECT id FROM customer WHERE mobile=%s;" % mobile)

    # 使用 fetchone() 方法获取单条数据.
    cus_id = cursor.fetchone()
    # 更新身份证信息
    updateID = """UPDATE `customer`
SET 
 `nric_no` = '%s',
 `nric_front_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151913_1617439745.jpg',
 `nric_back_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151905_1457736902.jpg',
 `fullface_pic` = 'http://p.lanlingdai.com/201903/null_9E5A18E4_front_JPEG_20190314_151856_138829020.jpg'
WHERE
    (`mobile` = '%s');""" % (nric_no, mobile)
    cursor.execute(updateID)
    db.commit()
    ###插入身份证认证状态
    update_status = """INSERT INTO `vn-loan`.`customer_certify_info` (
    `customer_id`,
    `credit_source_id`,
    `credit_source_ver`,
    `certify_type`,
    `certify_result`,
    `must_passed`,
    `failure_reason`,
    `failure_code`,
    `report`,
    `report_status`,
    `repull_times`,
    `expired_at`,
    `created_at`,
    `updated_at`
)
VALUES
    (

        '%s',
        '128',
        '',
        '10',
        '2',
        '1',
        NULL,
        NULL,
        '',
        '0',
        '0',
        '2019-09-10 15:50:19',
        '2019-03-14 15:51:51',
        '2019-03-14 15:50:19'
    );
""" % cus_id
    cursor.execute(update_status)
    db.commit()
    # 关闭数据库连接
    db.close()


def audit_order():
    browser = webdriver.Chrome()
    browser.get(houtai)
    browser.maximize_window()
    browser.implicitly_wait(3)
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[1]/input').send_keys('13818835751')
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[2]/input').send_keys('12345678')
    browser.find_element_by_id('auLogin').click()
    time.sleep(1)
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[4]/button').click()
    time.sleep(3)
    browser.find_element_by_class_name('change-lang').click()
    time.sleep(3)
    browser.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/ul/li[2]/a').click()
    time.sleep(2)
    browser.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/div[2]/div/div/div/div/div/div/table/tbody/tr[1]/td[8]/span/button').click()
    time.sleep(4)
    audit_button = browser.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/div[3]/div[2]/div/div[2]/div/button")
    browser.execute_script("arguments[0].scrollIntoView()", audit_button)
    browser.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div[3]/div[2]/div/div[1]/div/textarea').send_keys('测试专用审核,如有雷同,实属巧合!!!!!!')
    time.sleep(1)
    audit_button.click()
    time.sleep(5)
    browser.quit()
    print('订单审核通过!!!')

def make_loan():
    print('开始放款。。。。。。。。')
    browser = webdriver.Chrome()
    browser.get(houtai)
    browser.maximize_window()
    browser.implicitly_wait(3)
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[1]/input').send_keys('13818835751')
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[2]/input').send_keys('12345678')
    browser.find_element_by_id('auLogin').click()
    time.sleep(1)
    browser.find_element_by_xpath('/html/body/div/div/div[1]/div[1]/ul/li[4]/button').click()
    time.sleep(3)
    browser.find_element_by_class_name('change-lang').click()
    time.sleep(3)
    browser.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/ul/li[4]/a').click()#放款管理
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="content"]/div/div/div/div[2]/div[2]/div/ul/li[2]/div/span/span').click()#批次管理
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="sub2$Menu"]/li/a').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="content"]/div/div/div/div[3]/div/div[1]/div/form/div[2]/div/div/div/button[2]').click()#新增放款批次
    time.sleep(2)
    browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[2]/form/div/div/div[1]/div[2]/div/div/div/span').click()#选择渠道
    time.sleep(1)
    browser.find_element_by_css_selector('body > div:nth-child(4) > div > div > div > ul > li:nth-child(1)').click()
    time.sleep(1)
    browser.find_elements_by_class_name('ant-input-number-input')[0].send_keys(18)
    browser.find_elements_by_class_name('ant-input-number-input')[1].send_keys(18)
    browser.find_element_by_css_selector('body > div:nth-child(3) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.ant-btn-primary.ant-btn-lg').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="content"]/div/div/div[3]/div/div/button[2]').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div/div/table/tbody/tr[1]/td[16]/span/button[3]').click()#回传结果
    time.sleep(1)
    browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[2]/form/div[2]/div/div[2]/div/span/div[1]/span/button').click()#上传文件
    time.sleep(2)
    k = PyKeyboard()
    k.type_string('C:\workspace\Autotest\psa.csv')
    k.press_key(k.enter_key)
    time.sleep(1)
    k.press_key(k.enter_key)
    time.sleep(2)
    browser.find_element_by_css_selector('body > div:nth-child(3) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.upload-demo-start.ant-btn-primary').click()#上传
    time.sleep(2)
    browser.find_element_by_css_selector('body > div:nth-child(3) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.ant-btn-lg').click()#返回
    browser.refresh()
    time.sleep(2)
    result=browser.find_element_by_xpath('//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div/div/table/tbody/tr[1]/td[13]/div/span').text
    print('放款%s'%result)
    time.sleep(3)
    browser.quit()


def overtime_loan(mobile):
    for i in range(7, -1, -1):
        print('离逾期还有%s天' % i)
        time.sleep(1)
    print('超过7天,开始逾期')
    overdays=random.randint(0,20)
    # overdays=8
    print('逾期天数为%s'%overdays)
    now = datetime.datetime.now()
    print(now)
    delta = datetime.timedelta(days=overdays)
    delta_1=datetime.timedelta(days=7)
    print(delta)
    n_days = now - delta
    f_days = n_days - delta_1
    f_day= f_days.strftime('%Y-%m-%d %H:%M:%S')
    new=n_days.strftime('%Y-%m-%d %H:%M:%S')
    print('到期日期:%s,放款日期:%s'%(new,f_day))
# #########更新逾期日期##############
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan", port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 更新身份证信息
    plan = """UPDATE `loan_repay_plan`
SET
 `due_date` = '%s'

WHERE
    `customer_mobile` = '%s';""" % (new,mobile)
    cursor.execute(plan)
    db.commit()
    ###插入身份证认证状态
    order = """UPDATE `loan_order`
SET
 `last_due_date` = '%s',
  `paid_at` = '%s'
WHERE
    `customer_mobile` = '%s';""" % (new,f_day,mobile)
    cursor.execute(order)
    db.commit()
    # 关闭数据库连接
    db.close()
#######################启动任务脚本更新逾期天数以及逾期金额#############

    browser = webdriver.Chrome()
    browser.get(renwu)
    browser.maximize_window()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/div[2]/button').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a').click()
    time.sleep(5)
    browser.find_element_by_xpath('/html/body/div[2]/aside/section/ul/li[2]/a/span').click()
    time.sleep(1)
    Select(browser.find_element_by_id("jobGroup")).select_by_visible_text("【vietnam-越南】")
    browser.find_element_by_id('searchBtn').click()
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="307"]/button[1]').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a[1]').click()
    time.sleep(2)
    print('逾期执行成功!!!')
    browser.quit()
    return overdays

def collection(mobile,overdays):
    ################入催##################
    browser = webdriver.Chrome()
    browser.get(renwu)
    browser.maximize_window()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/div[2]/button').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a').click()
    time.sleep(5)
    browser.find_element_by_xpath('/html/body/div[2]/aside/section/ul/li[2]/a/span').click()
    time.sleep(1)
    Select(browser.find_element_by_id("jobGroup")).select_by_visible_text("【vietnam】催收")
    browser.find_element_by_id('searchBtn').click()
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="320"]/button[1]').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a[1]').click()
    time.sleep(1)
    browser.find_element_by_css_selector('.layui-layer-btn0').click()
    time.sleep(2)
    if overdays==0:
        print('S0分案执行中。。。。。')
        browser.find_element_by_xpath('//*[@id="315"]/button[1]').click()
    elif overdays in range(1,8):
        print('S1分案执行中。。。。。')
        browser.find_element_by_xpath('//*[@id="316"]/button[1]').click()
    elif overdays in range(8, 61):
        print('S2分案执行中。。。。。')
        browser.find_element_by_xpath('//*[@id="318"]/button[1]').click()
    elif overdays > 61:
        print('M3分案执行中。。。。。')
        browser.find_element_by_xpath('//*[@id="319"]/button[1]').click()
    time.sleep(2)
    browser.find_element_by_css_selector('.layui-layer-btn0').click()
    time.sleep(2)
    browser.find_element_by_css_selector('.layui-layer-btn0').click()
    time.sleep(1)

############################分案到指定账户之中###########
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    batch_no=datetime.datetime.now().strftime('%Y%m%d%H%M%S')+'888'
    db = pymysql.connect(host="10.0.0.9", user="dev-vn-rw", password="Vnro@123456", db="vn-loan", port=3306)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 更新身份证信息
    order_no_sql = """SELECT loan_order_no FROM `loan_order` WHERE customer_mobile='%s';""" % mobile
    cursor.execute(order_no_sql)
    time.sleep(1)
    order_no = cursor.fetchone()[0]
    print(order_no)
    collect_order="""UPDATE `collect_order`
SET
 `assign_stage` = 'S2',
 `assign_batch_no` = '%s',
 `assign_batch_date` = '%s',
 `assign_company_id` = '12',
 `assign_company_name` = '委外2',
 `assign_department_id` = '120',
 `assign_department_name` = '催收一部',
 `assign_group_stage_id` = '1201',
 `assign_group_stage_name` = 'S2',
 `assign_group_id` = '12001',
 `assign_group_name` = 'S2组一',
 `assignee_uid` = '14',
 `assignee_uname` = '周佳毅'
WHERE
    `loan_order_no` = '%s';
"""%(batch_no,now,order_no)
    cursor.execute(collect_order)
    db.commit()

#############################登录催收系统操作催收###############
    browser = webdriver.Chrome()
    browser.get(rucui)
    browser.maximize_window()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/form/input[1]').send_keys('13818835751')
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/form/input[2]').send_keys('123456')
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/div[1]/label/span[1]/span').click()
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/button/span').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[1]/div[2]/div/i[2]').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[2]/div[2]/div[3]/table/tbody/tr[1]/td[15]/div/div').click()
    time.sleep(2)
    all_collection=browser.find_element_by_xpath('//*[@id="top-tab"]/div[2]/div[1]/div/div[3]/div[4]/span[2]/span').text
    overdays=browser.find_element_by_xpath('//*[@id="top-tab"]/div[2]/div[1]/div/div[4]/div[1]/span[2]').text
    need_payment=browser.find_element_by_xpath('//*[@id="top-tab"]/div[2]/div[1]/div/div[4]/div[2]/span[2]').text
    overfee=browser.find_element_by_xpath('//*[@id="top-tab"]/div[2]/div[1]/div/div[4]/div[3]/span[2]').text
    time.sleep(2)
    print(browser.find_element_by_class_name('loanOrderId').text)
    order_id=re.findall('\d+',browser.find_element_by_class_name('loanOrderId').text)[0]
    print(order_id)
    qudian=browser.find_element_by_xpath('//*[@id="%s"]/div[1]/div[1]/div/div/div/label[1]/span'%order_id)
    browser.execute_script("arguments[0].scrollIntoView()", qudian)
    browser.find_element_by_xpath('//*[@id="%s"]/div[1]/div[1]/div/div/div/label[1]/span'%order_id).click()
    browser.find_element_by_xpath('//*[@id="%s"]/div[2]/div/div/div/div/label[2]/span'%order_id).click()
    browser.find_element_by_xpath('//*[@id="%s"]/div[3]/div/div/div/div/textarea'%order_id).send_keys('测试专用催收,如有雷同,实属巧合!!!!!!')
    browser.find_element_by_xpath('//*[@id="%s"]/div[4]/div/button/span'%order_id).click()
    print('催收操作完成!!!!!!')
    return all_collection,overdays,overfee,need_payment

def huankuan_job():
    browser = webdriver.Chrome()
    browser.get(renwu)
    browser.maximize_window()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/div[2]/button').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a').click()
    time.sleep(5)
    browser.find_element_by_xpath('/html/body/div[2]/aside/section/ul/li[2]/a/span').click()
    time.sleep(1)
    Select(browser.find_element_by_id("jobGroup")).select_by_visible_text("【vietnam】还款")
    browser.find_element_by_id('searchBtn').click()
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="322"]/button[1]').click()
    time.sleep(2)
    browser.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a[1]').click()
    time.sleep(1)
    browser.find_element_by_css_selector('.layui-layer-btn0').click()
    time.sleep(2)





def delay_order():
    browser = webdriver.Chrome()
    browser.get(rucui)
    browser.maximize_window()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/form/input[1]').send_keys('13818835751')
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/form/input[2]').send_keys('123456')
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/div[1]/label/span[1]/span').click()
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[2]/button/span').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[1]/div[2]/div/i[2]').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="el-menu-vertical-demo"]/li[1]/div/i[1]').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="el-menu-vertical-demo"]/li[1]/div').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="el-menu-vertical-demo"]/li[1]/ul/li/ul/li[3]/div/i').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="el-menu-vertical-demo"]/li[1]/ul/li/ul/li[3]/ul/li/ul/li[2]/span[1]').click()
    time.sleep(1)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[2]/div[2]/div[3]/table/tbody/tr[1]/td[16]/div/div').click()


def repay_datas ():
    now = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')
    from copy import deepcopy
    datalist = []
    filename = 'repay.csv'
    with open(filename,encoding='utf-8') as f:
        reader = csv.reader(f)
        data1=list(reader)[1]
        # del(data[0])
        print(data1)
    for i in range(1,10):
        t = time.time()
        now1 = int(round(t * 1000))
        data=deepcopy(data1)
        # print(datalist)
        # sn=list(reader)[1][0]+1
        # pici=list(reader)[1][1]+1
        # liushui=list(reader)[1][-1]+1
        data[2]=now
        data[0]=int(data[0])+i
        data[1]=now1
        data[-1]=now1+1
        datalist.append(data)
        time.sleep(0.01)
    print(datalist)
    save = pandas.DataFrame(datalist)
    save.to_csv(filename, mode='a', header=False,index=False)


def repay_datas ():
    now = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')
    from copy import deepcopy
    datalist = []
    filename = 'repay.csv'
    with open(filename,encoding='utf-8') as f:
        reader = csv.reader(f)
        data1=list(reader)[1]
        # del(data[0])
        print(data1)
    for i in range(1,10):
        t = time.time()
        now1 = int(round(t * 1000))
        data=deepcopy(data1)
        # print(datalist)
        # sn=list(reader)[1][0]+1
        # pici=list(reader)[1][1]+1
        # liushui=list(reader)[1][-1]+1
        data[2]=now
        data[0]=int(data[0])+i
        data[1]=now1
        data[-1]=now1+1
        datalist.append(data)
        time.sleep(0.01)
    print(datalist)
    save = pandas.DataFrame(datalist)
    save.to_csv(filename, mode='a', header=False,index=False)


# repay_data()

 

posted @ 2019-04-25 16:48  太阳神的意志  阅读(2057)  评论(0编辑  收藏  举报