直接上代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time,re,requests
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
import os,cv2
import sys
path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(path)
class main():
def __init__(self):
self.url = 'https://static-mp-dc3bab1b-06be-41ca-9070-ab7368c17ae5.next.bspapp.com/'
self.distance = 0
self.left = 0
self.track = []
# 启动浏览器
def Launch_browser(self):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# self.driver = webdriver.Chrome(options=options)
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver, 10, 0.5)
self.driver.get(self.url)
self.driver.find_element(By.XPATH,'/html/body/button').click()
# 等待className为geetest_slider_button的元素在元素表中出现
time.sleep(5)
element = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'tcaptcha-transform'))
)
time.sleep(5)
# 切换到iframe
# 假设iframe有id或者其他属性,可以通过这些属性定位
self.iframe = self.driver.find_element(By.ID,'tcaptcha_iframe_dy')
self.driver.switch_to.frame(self.iframe)
self.slider = self.driver.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/div[7]')
self.sliderImg = self.driver.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/div[1]/div[2]/div')
sliderImg_background_image_url = self.sliderImg.value_of_css_property('background-image')
sliderImg_background_image_url = sliderImg_background_image_url[5:len(sliderImg_background_image_url) - 3]
resp = requests.get(sliderImg_background_image_url)
with open('./sliderImg.png', 'wb') as f:
f.write(resp.content)
slider_background_image_url = self.slider.value_of_css_property('background-image')
slider_background_image_url = slider_background_image_url[5:len(slider_background_image_url) - 3]
resp = requests.get(slider_background_image_url)
with open('./slider.png', 'wb') as f:
f.write(resp.content)
# 150,270
# 500,600
image = Image.open('./slider.png')
bg = image.crop([130, 479, 272, 622])
bg.save('slider.png')
import ddddocr
det = ddddocr.DdddOcr(det=False, ocr=True, show_ad=False)
with open('slider.png', 'rb') as f:
target_bytes = f.read()
with open('sliderImg.png', 'rb') as f:
background_bytes = f.read()
res = det.slide_match(target_bytes, background_bytes, simple_target=True)
print(res)
self.distance = res['target'][0]
self.left = self.slider.value_of_css_property('left').split('px')[0]
self.left = eval(self.left)
xoffset = int(self.distance * 0.51)
print(xoffset)
verify_img = cv2.imread('sliderImg.png')
# 调用函数,得到x坐标
x = get_pos(verify_img)
x = int(x * 0.51) - 30
# 实现拖拽滑动
ActionChains(self.driver).click_and_hold(self.slider).perform()
ActionChains(self.driver).move_by_offset(x, 0).perform()
ActionChains(self.driver).release().perform()
self.quit()
# 关闭浏览器
def quit(self):
time.sleep(10)
self.driver.quit()
# main方法
def main(self):
self.Launch_browser()
# self.cjy()
# self.move()
# self.quit()
# 定义一个处理图片缺口的函数,最后是返回x坐标,滑块移动不需要y坐标
def get_pos(image):
# 首先使用高斯模糊去噪,噪声会影响边缘检测的准确性,因此首先要将噪声过滤掉
blurred = cv2.GaussianBlur(image, (5, 5), 0, 0)
# 边缘检测,得到图片轮廓
canny = cv2.Canny(blurred, 200, 400) # 200为最小阈值,400为最大阈值,可以修改阈值达到不同的效果
# 轮廓检测
# cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),所以读取的图像要先转成灰度的,再转成二值图,此处canny已经是二值图
# contours:所有的轮廓像素坐标数组,hierarchy 轮廓之间的层次关系
contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print(contours, hierarchy)
for i, contour in enumerate(contours): # 对所有轮廓进行遍历
M = cv2.moments(contour) # 并计算每一个轮廓的力矩(Moment),就可以得出物体的质心位置
# print(M)
if M['m00'] == 0:
cx = cy = 0
else:
# 得到质心位置,打印这个轮廓的面积和周长,用于过滤
cx, cy = M['m10'] / M['m00'], M['m01'] / M['m00']
print(cv2.contourArea(contour), cv2.arcLength(contour, True))
# 判断这个轮廓是否在这个面积和周长的范围内
if 5000 < cv2.contourArea(contour) < 8000 and 300 < cv2.arcLength(contour, True) < 500:
print(cx)
if cx < 300:
continue
print(cv2.contourArea(contour))
print(cv2.arcLength(contour, True))
# 外接矩形,x,y是矩阵左上点的坐标,w,h是矩阵的宽和高
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # 画出矩行
# cv2.imshow('image', image)
cv2.imwrite('111.jpg', image) # 保存
return x
return 0
if __name__ == '__main__':
ma = main()
ma.main()
效果展示:
Tip:用的是腾讯提供的web端接入示例
本文转载自: https://blog.csdn.net/weixin_59685936/article/details/142058436
版权归原作者 某公司摸鱼前端 所有, 如有侵权,请联系我们删除。
版权归原作者 某公司摸鱼前端 所有, 如有侵权,请联系我们删除。