selenium辅助自动抽奖

  • tesseract-orc 验证码识别
  • selenium webdriver for python

相关:AccessibilityService是一个辅助类,可以监听我们手机的焦点,窗口变化,按钮点击等等。实现它的服务需要在手机设置里面->辅助功能在这里面找到你自己实现的辅助类,然后打开它就可以进行我们一系列的监听,这个功能google的出发点是给那些肢体上有障碍的人使用的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.ui as ui
import pytesseract
from PIL import Image
import time

driver = None
login_handle = None
cj_handle = None

def run():
    login()
    doit()

def doit():
    global cj_handle
    all_handles = driver.window_handles
    for handle in all_handles:
        print "handle:", handle
        if handle != login_handle:
            print "handle:", handle, " login_handle:", login_handle
            cj_handle = handle
            driver.switch_to_window(cj_handle)
    time.sleep(3)
    print driver.page_source
    print "open [", cj_handle, "] start"
    wait = ui.WebDriverWait(driver, 5)
    wait.until(lambda dr: dr.find_element_by_id('act5245').is_displayed())
    print "open [", cj_handle, "] OK"
    driver.find_element_by_id("act5245").click()
    print "click 立刻抽奖"

def login():
    global login_handle
    url='http://www.yooyo.com/event/m/cbk0912/login/5243.html'
    driver.get(url)
    login_handle = driver.current_window_handle
    wait = ui.WebDriverWait(driver, 5)
    wait.until(lambda dr: dr.find_element_by_id('img_captcha_phone').is_displayed())
    print "open [handle:", login_handle, " url:", url, " title:", driver.title, "] OK"

    driver.save_screenshot('aa.png')  #截取当前网页,该网页有我们需要的验证码
    #imgelement = driver.find_element_by_xpath("//img[@id='img_captcha_phone']")  #定位验证码
    imgelement = driver.find_element_by_xpath("//img[1]")  #定位验证码
    location = imgelement.location  #获取验证码x,y轴坐标
    size=imgelement.size  #获取验证码的长宽
    rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #写成我们需要截取的位置坐标
    i=Image.open("aa.png") #打开截图
    codepic=i.crop(rangle)  #使用Image的crop函数,从截图中再次截取我们需要的区域
    codepic.save('codepic.jpg')
    #qq=Image.open('codepic.jpg')
    #text=pytesseract.image_to_string(qq).strip() #使用image_to_string识别验证码
    #print "decode:[", text, "]"

    phonenum=raw_input("enter phonenum:")
    piccode=raw_input("enter the piccode(codepic.jpg):")
    driver.find_element_by_id('account').send_keys(phonenum)
    driver.find_element_by_name('captcha_code').send_keys(piccode)
    driver.find_element_by_id('btnCaptcha').click()
    msgcode=raw_input("enter the msgcode:")
    driver.find_element_by_name('captcha_code').send_keys(msgcode)
    print "phonenum", phonenum, "piccode:", piccode, " msgcode:", msgcode
    driver.find_element_by_id('login').click()
    print "login success"
    time.sleep(3)

def opendriver():
    global driver
    driver = webdriver.PhantomJS()
    driver.maximize_window()  #将浏览器最大化

def main():
    opendriver()
    run()
    driver.quit()

if __name__ == '__main__':
    main()