有参考其他的文章,自己也写一个。里面有注释。
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from time import sleep
import base64
from PIL import Image
from io import BytesIO
import cv2
import numpy as np
from PIL import Image as Im
import time
class demo3:
def __init__(self):
self.url = 'https://passport.jd.com/new/login.aspx'
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver, 20)
self.username = "账号"
self.password = "密码"
self.pianyi_x=0
self.pianyi_y = 0
def agreement_inputPhone(self):
self.driver.get(self.url)
self.driver.maximize_window()
sleep(1)
self.driver.find_elements_by_xpath('//div[@class="login-tab login-tab-r"]//a')[0].click()
sleep(3)
self.inputuser = self.wait.until(EC.presence_of_element_located((By.ID, "loginname")))
self.inputuser.send_keys(self.username)
self.inputpwd = self.wait.until(EC.presence_of_element_located((By.ID, "nloginpwd")))
self.inputpwd.send_keys(self.password)
sleep(1)
login_button = self.driver.find_elements_by_xpath('//div[@class="login-btn"]/a[@id="loginsubmit"]')[0]
login_button.click()
sleep(3)
def decode_image(self):
bigimg=self.driver.find_element_by_xpath('//div[@class="JDJRV-bigimg"]/img').get_attribute('src')
image1_bytes = base64.urlsafe_b64decode(bigimg.split(',')[-1])
f = open('big.png', 'wb')
f.write(image1_bytes)
f.close()
smallimg = self.driver.find_element_by_xpath('//div[@class="JDJRV-smallimg"]/img').get_attribute('src')
image2_bytes = base64.urlsafe_b64decode(smallimg.split(',')[-1])
f2 = open('small.png', 'wb')
f2.write(image2_bytes)
f2.close()
def find_pic(self,bigPath="big.png",smallPath="small.png"):
otemp =bigPath
oblk = smallPath
target = cv2.imread(otemp, 0)
template = cv2.imread(oblk, 0)
w, h = target.shape[::-1]
ws,hs=template.shape[::-1]
targ = 'targ.jpg'
temp = 'temp.jpg'
cv2.imwrite(temp, template)
cv2.imwrite(targ, target)
target = cv2.imread(targ)
target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
target = abs(255 - target)
cv2.imwrite(targ, target)
target = cv2.imread(targ)
template = cv2.imread(temp)
result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
y,x = np.unravel_index(result.argmax(), result.shape)
print(max_loc,x,y)
image = Im.open(otemp)
xy = (x , y , x +ws, y +hs)
imagecrop = image.crop(xy)
imagecrop.save("new_image.png")
self.pianyi_x=x
self.pianyi_y=y
bigc=cv2.imread('big.png')
cv2.circle(bigc,(x,y),20,(0,0,255),0)
def move_drive(self):
huakuai=self.driver.find_element_by_xpath('//div[@class="JDJRV-slide-bg "]/div[@class="JDJRV-slide-inner JDJRV-slide-btn"]')
tracks=self.get_track(self.pianyi_x)
print('和为'+str(sum(tracks)))
ActionChains(self.driver).click_and_hold(huakuai).perform()
for x in tracks:
ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(0.5)
ActionChains(self.driver).release().perform()
time.sleep(3)
def get_track(self,distance):
track = []
current = 0
mid = distance * 9 / 10
t = 0.2
v = 1
while current < distance:
if current < mid:
a = 10
else:
a = -3
v0 = v
v = v0 + a * t
move = v0 * t + 1 / 2 * a * t * t
current += move
track.append(round(move))
track[-1]=track[-1]-sum(track)+self.pianyi_x
print(track)
return track
if __name__ == '__main__':
demo=demo3()
demo.agreement_inputPhone()
while demo.url==demo.driver.current_url:
demo.decode_image()
demo.find_pic()
demo.move_drive()