Selenium定时签到程序实现

发表于:2017-12-28 10:19

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

 作者:罗罗的奇幻之旅    来源:博客园精华区

  定时任务
# -*- 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_win32\chromedriver.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()

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号