Python + Selenium 实现模拟登录jd实例分享

发布时间:2024年01月02日

1. 前言

最近有点时间,就随便找点东西弄弄,倒也碰到了一些问题,在此记录下

2. 环境

Python3.11.3 + selenium4.9.1 + opencv4.7 + PyAutoGUI0.9.54 + windows11

3. 开始

3.1 账号密码输入

进入登录页面,登录方式有两种,这里直接定位点击账号登录即可

1

2

3

4

5

6

7

8

9

10

11

12

# 进入登入页面

self.driver.get(self.config.login_url)

WebDriverWait(self.driver, 10).until(EC.url_to_be(self.config.login_url))

self.driver.maximize_window()

?????????

# 点击账号登录

WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="login-tab login-tab-r"]/a')))

self.driver.find_element(By.XPATH, '//*[@class="login-tab login-tab-r"]/a').click()

# 账号密码输入

self.driver.find_element(By.ID, "loginname").send_keys(self.user_info.username)

self.driver.find_element(By.ID, "nloginpwd").send_keys(self.user_info.password)

3.2 通过验证码

3.2.1 验证码图片下载

看到验证码的图片是base64格式的,可以通过src属性来获取,然后直接转成cv图片格式即可

1

2

3

4

5

6

7

8

9

bigimg_b64 = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-bigimg"]/img').get_attribute('src')

bigimg_data = base64.b64decode(bigimg_b64.replace('data:image/png;base64,', ''))

bigimg_array = np.frombuffer(bigimg_data, np.uint8)

bigimg_img = cv2.imdecode(bigimg_array, cv2.COLOR_RGB2BGR)

smallimg_b64 = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-smallimg"]/img').get_attribute('src')

smallimg_data = base64.b64decode(smallimg_b64.replace('data:image/png;base64,', ''))

smallimg_array = np.frombuffer(smallimg_data, np.uint8)

smallimg_img = cv2.imdecode(smallimg_array, cv2.COLOR_RGB2BGR)

3.2.2 滑块需要移动的距离计算


?


?

这里可以用opencv来做,正确率还不错,而且还简单,直接把两张验证码图片经过灰度后,进行模板匹配即可,不过最后的结果还需要根据网页元素的尺寸进行调整

1

2

3

4

5

6

7

8

9

10

# 灰度化

bigimg_gray = cv2.cvtColor(bigimg_img, cv2.COLOR_BGR2GRAY)

smallimg_gray = cv2.cvtColor(smallimg_img, cv2.COLOR_BGR2GRAY)

# 模板匹配

result = cv2.matchTemplate(bigimg_gray, smallimg_gray, cv2.TM_CCOEFF_NORMED)

minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)

# 移动距离对应到网页需要缩放(网页显示的图片和实际图片存在一定的比例差异)

x = minLoc[0] * (278.4 / 360.0)

3.2.3 定位滑动按钮

之前一直使用selenium的ActionChains来操作滑块按钮,但是一直通不过,应该是jd有针对selenium有检测,后面参考了网上可以使用PyAutoGUI来控制鼠标来滑动,那就需要先定位到滑块的坐标,但是通过selenium获取的坐标还需要调整一下PyAutoGUI才能正确的定位到

1

2

3

4

5

WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@class="JDJRV-slide-inner JDJRV-slide-btn"]')))

slide_btn = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-slide-inner JDJRV-slide-btn"]')

# TODO 网页元素位置映射到pyautogui会有一定缩放

offset_x = slide_btn.location.get('x') * 1.30

offset_y = slide_btn.location.get('y') * 1.75

3.2.4 模拟滑动

滑的时候发现上面opencv计算的移动距离还是有些偏差,还需要做些调整,而且滑动也得尽量拟人化,不然滑对了也通不过

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

# 直接滑到目标位置--会很难通过验证(用来调试移动距离是否正确)

# pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))

# pyautogui.mouseDown()

# pyautogui.moveTo(offset_x + x * 1.25, offset_y, duration=0.28)

# pyautogui.mouseUp()

# TODO 根据验证码原图计算的移动距离也需要调一下缩放

x = x * 1.25

# 鼠标移动到滑块

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(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.mouseUp()

3.2.5 后续处理

到此基本上模拟登陆就完成了,避免失败,可以加个循环,滑块未通过时继续下一张,再做一些是否登录成功的验证就欧克啦。

?现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走!?希望能帮助到你!【100%无套路免费领取】

文章来源:https://blog.csdn.net/Xsk215/article/details/135340473
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。