'''
Created on 2021年11月16日
@author: ichiro
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image, ImageEnhance
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import cv2
import numpy as np
from io import BytesIO
import time, requests, os
import pyautogui
from win32gui import Rectangle
import imghdr
from selenium.common.exceptions import NoSuchElementException
base_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
class crackSlider():
def __init__(self):
super(crackSlider, self).__init__()
driverpath = base_path + os.path.sep + 'driver' + os.path.sep + 'chromedriver.exe'
# 实际地址
self.url = 'www.baidu.com'
self.driver = webdriver.Chrome(driverpath)
self.wait = WebDriverWait(self.driver, 20)
self.zoom = 2
def open(self):
self.driver.get(self.url)
self.driver.find_element_by_xpath('//*[@id="pane-first"]/form/div[1]/div[1]/div/div/input').send_keys("pant")
self.driver.find_element_by_xpath('//*[@id="pane-first"]/form/div[1]/div[2]/div/div/input').send_keys("qeqew123456.")
self.driver.find_element_by_xpath('//*[@id="pane-first"]/form/div[2]/button').click()
time.sleep(3)
def get_pic(self):
time.sleep(2)
target = self.driver.find_element_by_class_name("floor1")
# 执行鼠标动作
actions = ActionChains(self.driver)
# 找到图片后右键单击图片
actions.context_click(target)
actions.perform()
time.sleep(1)
# 发送键盘按键,根据不同的网页,
# 右键之后按对应次数向下键,
# 找到图片另存为菜单
pyautogui.typewrite(['down', 'down', 'enter', 'enter'])
# 单击图片另存之后等1s敲回车
time.sleep(1)
pyautogui.typewrite(['enter'])
time.sleep(5)
#=======================
target_filename = r'C:\Users\ichiro\Downloads\下载.png'
target_img = Image.open(target_filename)
target_img = target_img.convert("RGB")
target_img.save('target.jpg')
#-------------------------
os.remove(target_filename)
local_img = Image.open('target.jpg')
size_loc = local_img.size
self.zoom = 300 / int(size_loc[0])
def get_tracks(self, distance):
distance += 10
v = 0
t = 0.2
forward_tracks = []
current = 0
mid = distance * 3 / 5
while current < distance:
if current < mid:
a = 2
else:
a = -3
s = v * t + 0.5 * a * (t ** 2)
v = v + a * t
current += s
forward_tracks.append(round(s))
back_tracks = [-1, -1, -1, -1, -1, -1, -1, -1]
# back_tracks = [-2,-2,-1,-1,-1,-1]
return {'forward_tracks':forward_tracks, 'back_tracks':back_tracks}
def matchOnShape(self, target):
# 载入原图
img = cv2.imread(target, 0)
# 在下面这张图像上作画
target_gray = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
imgHLS = cv2.cvtColor(target_gray, cv2.COLOR_BGR2HLS)
mask = cv2.inRange(imgHLS, np.array([0, 250, 0]), np.array([255, 255, 255]))
white_mask = cv2.bitwise_and(img, img, mask=mask)
# 二值化图像
_, thresh = cv2.threshold(white_mask,# 转换为灰度图像,
130, 255, # 大于130的改为255 否则改为0
cv2.THRESH_BINARY) # 黑白二值化
# 搜索轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = np.squeeze(hierarchy)
i = 0
distance = 0
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
if w > 30 and w < 50 and h > 30 and h < 50:
distance = x
i += 1
# print("=====================================")
#os.remove(target)
return distance
def moveSlider(self):
target = 'target.jpg'
a = 1
while a == 1:
self.get_pic()
distance = self.matchOnShape(target)
slider = self.driver.find_element_by_xpath('//div[@class="el-tooltip el-slider__button"]')
ActionChains(self.driver).click_and_hold(slider).perform()
ActionChains(self.driver).move_by_offset(xoffset=distance, yoffset=0).release(slider).perform()
try:
self.driver.find_element_by_xpath('//div[@class="notice fail"]').text
except NoSuchElementException:
a = 2
if __name__ == '__main__':
cr = crackSlider()
cr.open()
cr.moveSlider()