Skip to content

Instantly share code, notes, and snippets.

@jackhuntcn
Created June 11, 2018 09:51
Show Gist options
  • Save jackhuntcn/fcf9d8eb2e5fa0b21b99970bbaceac1d to your computer and use it in GitHub Desktop.
Save jackhuntcn/fcf9d8eb2e5fa0b21b99970bbaceac1d to your computer and use it in GitHub Desktop.
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import base64
import time
import numpy as np
import cv2
driver = webdriver.Firefox(executable_path="./geckodriver")
url = "http://h5.gaoshouyou.com/h5_game/heibai/index.html"
driver.set_window_size(400, 600)
driver.get(url)
canvas = driver.find_element_by_id("linkScreen")
# 抓取 canvas base64
def get_canvas():
global driver
get_base64_script = '''var canvas = document.getElementById("linkScreen"); return canvas.toDataURL().substring(22);'''
img_base64 = driver.execute_script(get_base64_script)
img_decode = base64.b64decode(img_base64)
img_array = np.fromstring(img_decode, np.uint8)
img = cv2.imdecode(img_array, cv2.COLOR_BGR2GRAY)
return img
# 图片处理
kernel = np.ones((10,10), np.uint8)
def process_img(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 60, 255, 0)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
return opening
# 找到第三排的黑块点击位置
def analy(img):
x = None
centers = [(x, 300) for x in range(40, 320, 80)]
for center in centers:
x, y = center
block = img[280:320,x-20:x+20]
rows, _ = np.where(block==0)
if rows.shape[0] == 1600:
return x
# 点击操作
def click_canvas(x):
global driver
global canvas
if x:
ActionChains(driver).move_to_element_with_offset(canvas, x, 300).click().perform()
k = 0
while k < 200:
img = get_canvas()
opening = process_img(img)
x = analy(opening)
click_canvas(x)
time.sleep(0.1)
k += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment