多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium定时签到程序实现-自动化测试

更新时间:2022-04-02 09:15:37 作者:多测师 浏览:110

  定时任务

Selenium定时签到程序实现-自动化测试

  # -*- coding: utf-8 -*-

  import time

  import os

  import sched

  import datetime

  from com.luoluo.v2ex.mengyou import toQuitAfterTen, toquit, tosign

  from com.luoluo.v2ex.randomTime import randomTime, night, morning

  schedule = sched.scheduler(time.time, time.sleep)

  def execute_command(cmd,flag, inc):

  strftime = time.time()

  if cmd < strftime and flag == 0:

  toquit()

  print('签退')

  elif cmd < strftime and flag == 1:

  tosign()

  print('签到')

  else:

  print("脚本持续运行中.....")

  schedule.enter(inc, 0, execute_command, (cmd,flag, inc))

  def main(cmd,flag, inc=60):

  # enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,

  # 给该触发函数的参数(tuple形式)

  schedule.enter(0, 0, execute_command, (cmd,flag, inc))

  schedule.run()

  if __name__ == '__main__':

  # 签退

  # random_time = '08:41:00'

  # random_time = '2017-12-27 ' + random_time

  random_time = night()

  random_time = time.strftime("%Y-%m-%d ", time.localtime())+random_time

  mktime = time.mktime(time.strptime(random_time, '%Y-%m-%d %H:%M:%S'))

  print(datetime.datetime.fromtimestamp(mktime))

  main(mktime, 0, 900)

  # 签到

  random_time = morning()

  random_time = time.strftime("%Y-%m-%d ", time.localtime()) + random_time

  # random_time = '10:41:00'

  # random_time = '2017-12-27 ' + random_time

  mktime = time.mktime(time.strptime(random_time, '%Y-%m-%d %H:%M:%S'))

  mktime = mktime + 86400.0

  print(datetime.datetime.fromtimestamp(mktime))

  main(mktime, 1, 900)

  随机产生时间

  # -*- coding: utf-8 -*-

  import random

  def time2seconds(t):

  h,m,s = t.strip().split(":")

  return int(h) * 3600 + int(m) * 60 + int(s)

  def seconds2time(sec):

  m,s = divmod(sec,60)

  h,m = divmod(m,60)

  return "%02d:%02d:%02d" % (h,m,s)

  def randomTime(st,et):

  sts = time2seconds(st) #sts==27000

  ets = time2seconds(et) #ets==34233

  return seconds2time(random.randint(sts, ets))

  def morning():

  st = "08:30:00"

  et = "09:00:00"

  return randomTime(st, et)

  def night():

  st = "22:00:00"

  et = "22:30:00"

  return randomTime(st, et)

  if __name__ == '__main__':

  time = randomTime()

  print(time)

  主程序

  # -*- coding: utf-8 -*-

  import pytesseract

  from PIL import Image,ImageEnhance

  import PIL.ImageOps

  from selenium import webdriver

  from time import sleep

  import urllib

  from selenium.webdriver import ActionChains

  from selenium.webdriver.common.keys import Keys

  driver = webdriver.Chrome(executable_path='D:chromedriver_win32chromedriver.exe')

  driver.get(

  "需要被签到的网址")

  def initTable(threshold=70):

  table = []

  for i in range(256):

  if i < threshold:

  table.append(0)

  else:

  table.append(1)

  return table

  def getImage():

  image =driver.find_element_by_class_name('yzmImg')

  driver.save_screenshot('screenshot.png')

  left = image.location['x']+3

  top = image.location['y']+4

  right = image.location['x'] + image.size['width']-3

  bottom = image.location['y'] + image.size['height']-4

  im = Image.open('screenshot.png')

  im = im.crop((left, top, right, bottom))

  im.save('screenshot.png')

  def getCode():

  im = Image.open('screenshot.png')

  #图片的处理过程

  im = im.convert('L')

  binaryImage = im.point(initTable(), '1')

  # im1 = binaryImage.convert('L')

  #

  # im2 = PIL.ImageOps.invert(im1)

  #

  # im3 = im2.convert('1')

  #

  # # out = im3.resize((120,38))

  # # im3.show()

  pytesseract.pytesseract.tesseract_cmd = 'D:\Users\soft\Tesseract-OCR\tesseract.exe'

  asd = pytesseract.image_to_string(binaryImage).replace(" ","")

  # print(asd)

  if(asd != None and asd !='' and len(asd) ==4):

  return asd

  def toQuitAfterTen():

  username = 'username'

  password = 'password'

  getImage()

  code = getCode()

  while code==None:

  image = driver.find_element_by_class_name('yzmImg')

  image.click()

  getImage()

  code = getCode()

  by_id = driver.find_element_by_id('username')

  by_id.clear()

  by_id.send_keys(username)

  element_by_id = driver.find_element_by_id('password')

  element_by_id.clear()

  element_by_id.send_keys(password)

  verifyCode = driver.find_element_by_id('verifyCode')

  verifyCode.clear()

  verifyCode.send_keys(code)

  driver.find_element_by_css_selector('img[src="/cas/pages/login/hos/login_btn.gif"]').click()

  def toquit(flags=0):

  while flags < 3:

  try:

  toQuitAfterTen()

  sleep(10)

  driver.find_element_by_id('outputButton').click()

  flags = 99

  print("签退成功!")

  except:

  sleep(4)

  flags += 1

  print("重试第:"+flags+"次")

  sleep(2)

  driver.quit()

  print(flags)

  def tosign(flags=0):

  while flags < 3:

  try:

  toQuitAfterTen()

  sleep(10)

  driver.find_element_by_id('inputButton').click()

  flags = 99

  print("签到成功!")

  except:

  sleep(4)

  flags += 1

  print("重试第: %s次" % flags)

  sleep(2)

  driver.quit()

  以上内容为大家介绍了自动化测试中的Selenium定时签到程序实现,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部