python自动化测试之破解图文验证码

发布时间:2025-03-18 02:47

在项目中使用自动化测试,保证代码质量。 #生活技巧# #学习技巧# #编程学习技巧#

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

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

import os

import subprocess

from PIL import Image

def get_captcha(driver, captcha_id, full_screen_img_path, captcha_img_path, captcha_final_path, txt_path, ocr_path):

    driver.save_screenshot(full_screen_img_path)

    element = driver.find_element_by_id(captcha_id)

    left = element.location['x']

    top = element.location['y']

    right = element.location['x'] + element.size['width']

    bottom = element.location['y'] + element.size['height']

    left, top, right, bottom = int(left), int(top), int(right), int(bottom)

    img = Image.open(full_screen_img_path)

    img = img.crop((left, top, right, bottom))

    img.save(captcha_img_path)

    img = Image.open(captcha_img_path)

    img_new = Image.new('P', img.size, 255)

    for x in range(img.size[1]):

        for y in range(img.size[0]):

            pix = img.getpixel((y, x))

            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:

                img_new.putpixel((y, x), 0)

    img_new.save(captcha_final_path, format='png')

    os.system(ocr_path)

    with open(txt_path, 'r') as f:

        if f.read():

            t = f.read().strip()

            if ' ' in t:

                t = t.replace(' ', '')

            if t.isdigit() and len(t) == 4:

                return t

            else:

                return 'fail'

def check_resp(result, msg):

    if msg in result:

        return 'pass'

    else:

        return 'failed'

def get_captcha(captcha_img_path, captcha_final_path, txt_path, ocr_path):

    img = Image.open(captcha_img_path)

    img_new = Image.new('P', img.size, 55)

    for x in range(img.size[1]):

        for y in range(img.size[0]):

            pix = img.getpixel((y, x))

            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:

                img_new.putpixel((y, x), 0)

    img_new.save(captcha_final_path, format='png')

    os.system(ocr_path)

    with open(txt_path, 'r') as f:

        if r.read():

            t = f.read().strip()

            if ' ' in t:

                t = t.replace(' ', '')

            if t.isdigit() and len(t) == 4:

                return t

            else:

                return fail

网址:python自动化测试之破解图文验证码 https://www.yuejiaxmz.com/news/view/829636

相关内容

让测试人头疼的web自动化之验证码识别彻底解决方案
Python 自动化Web测试
测试和开发工作必备的17个Python自动化代码
如何自动化解决或破解文字、图像、滑块、点选等验证码问题
Web自动化测试工具Selenium
八年 “自动化测试” 老鸟,写给 3
python 网页自动化操作
Python自动化任务
APP自动化测试工具:八款推荐解析
【书单】自动化测试书籍推荐?自动化测试书籍哪个好?笔者亲自看过后推荐!

随便看看