写在前面。
非原创,参考的是这篇文章,侵权转自己可见。
我只是在他的代码上做了稍微的修改,滑动的时候随缘划过,但亲测可过。
from urllib import request
from selenium import webdriver
import cv2
import random
import time
import pyautogui
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://passport.jd.com/new/login.aspx")
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[3]/a').click()
driver.find_element_by_xpath('//*[@id="loginname"]').send_keys('野蛮水蜜桃')
driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys('*********')
driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()
while True:
# 从网页上获取组件
target = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[1]/div[2]/div[1]/img')
template = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[1]/div[2]/div[2]/img')
# 获取模块的url路径
src1 = target.get_attribute("src")
src2 = template.get_attribute("src")
# 下载图片
request.urlretrieve(src1,"findPicimg1.jpg")
request.urlretrieve(src2,"img2.png")
pic1 = "findPicimg1.jpg"
pic2 = "img2.png"
# 读取图片
target_rgb = cv2.imread(pic1)
# 图片灰度化
target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
# 读取模块图片
template_rgb = cv2.imread(pic2, 0)
# 匹配模块位置
res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
# 获取最佳匹配位置
value = cv2.minMaxLoc(res)
# 返回最佳X坐标
x = value[2][0]
print(x)
w1 = cv2.imread('findPicimg1.jpg').shape[1]
w2 = target.size['width']
print(w1,w2)
x = x / w1 * w2
# # 按钮坐标
offset_x,offset_y = 1276,588
# pyautogui库操作鼠标指针
pyautogui.moveTo(offset_x, offset_y,
duration=0.1 + random.uniform(0, 0.1 + random.randint(1, 100) / 100))
pyautogui.mouseDown()
offset_y += random.randint(9, 19)
pyautogui.moveTo(offset_x + int(x * random.randint(15, 25) / 20), offset_y, duration=0.28)
offset_y += random.randint(-9, 0)
pyautogui.moveTo(offset_x + int(x * random.randint(17, 23) / 20), offset_y,
duration=random.randint(20, 31) / 100)
offset_y += random.randint(0, 8)
pyautogui.moveTo(offset_x + int(x * random.randint(19, 21) / 20), offset_y,
duration=random.randint(20, 40) / 100)
offset_y += random.randint(-3, 3)
pyautogui.moveTo(offset_x + int(x * random.randint(15, 20) / 20), offset_y,
duration=random.randint(20, 40) / 100)
offset_y += random.randint(-3, 3)
pyautogui.moveTo(x + offset_x + random.randint(-3, 3), offset_y,
duration=0.5 + random.randint(-10, 10) / 100)
offset_y += random.randint(-2, 2)
pyautogui.moveTo(x + offset_x + random.randint(-2, 2), offset_y,
duration=0.5 + random.randint(-3, 3) / 100)
pyautogui.moveTo(x + offset_x + random.randint(12, 17), offset_y,
duration=0.5 + random.randint(-3, 3) / 100)
pyautogui.mouseUp()
time.sleep(1)
题外话:为什么还有展示封面这种东西