大概流程
1、进入页面点击同意按钮。
2、得到滑块和滑块区域的xpath
3、定位滑块,设置按住事件,进行拖动
4、xpath定位汉字图片,获取屏幕截图,保存本地
5、超级鹰识别图片返回汉字对应的坐标,移动至坐标位置进行点击。需注意selenium 4.3版本在移动坐标时有问题,建议使用4.0版本。
关于selenium 和超级鹰的用法不做详细步骤。
工具版本
selenium 4.0.0
谷歌浏览器和驱动版本 111.0.5563.111
代码记录
# coding = utf-8
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from app.Chaojiying_Python.chaojiying import Chaojiying_Client
from selenium.webdriver.support.ui import WebDriverWaitUSERNAME = 'xxxxxxxx'
PASSWORD = 'xxxxxxxx'
SOFTID = xxxxx
class XieCheng:def __init__(self):# 打开chrome浏览器self.d = webdriver.Chrome()self.chaojiying = Chaojiying_Client(USERNAME, PASSWORD,SOFTID)self.wait = WebDriverWait(self.d,20)self.action = ActionChains(self.d) # 创建动作链对象self.d.maximize_window()self.d.implicitly_wait(10)self.action = ActionChains(self.d)self.url = 'https://passport.ctrip.com/user/reg/home'def open_url(self):# 打开携程网注册页面self.d.get(self.url)# 点击同意并继续self.d.find_element(By.XPATH, '//div[@class="pop_footer"]/a[@class="reg_btn reg_agree"]').click()# 定位到滑块按钮元素ele_button = self.d.find_element(By.XPATH, '//*[@id="slideCode"]/div[1]/div[2]')# 打印滑块按钮的宽和高print('滑块按钮的宽:', ele_button.size['width'])print('滑块按钮的高:', ele_button.size['height'])# 定位到滑块区域元素ele = self.d.find_element(By.XPATH, '//div[@class="cpt-bg-bar"]')# 打印滑块区域的宽和高print('滑块区域的宽:', ele.size['width'])print('滑块区域的高:', ele.size['height'])# 按住滑块不动self.action.click_and_hold(ele_button).perform()# 拖动滑块self.action.drag_and_drop_by_offset(ele_button, ele.size['width'], ele.size['height']).perform()# action.move_by_offset(xoffset=ele.size['width'], yoffset=0).perform()def downloadimg(self):img_element = self.d.find_element(By.XPATH,'//*[@id="slideCode-choose"]/div[2]')img_element.screenshot('img.png')return img_elementdef get_position(self):img_element = self.downloadimg()im = open('img.png', 'rb').read() # 以二进制的方式读取验证码result = self.chaojiying.PostPic(im, 9005)['pic_str'] # 9005获取验证码3-5坐标字典,并取出坐标值print(result)for index in result.split('|'): # 以"|"进行分割,得到一个列表,并循环出每一个字的坐标x = index.split(',')[0] # 得到x轴的坐标y = index.split(',')[1] # 得到y轴的坐标print(x,y)self.action.move_to_element_with_offset(img_element, int(x), int(y)).click().perform()# image:验证码的元素框;x:验证码的横轴;y:验证码的纵轴time.sleep(1)self.d.find_element(By.XPATH,'//*[@id="slideCode-choose"]/div[2]/div[4]/a').click()if __name__ == '__main__':api = XieCheng()api.open_url()api.get_position()