selenium学习笔记之模拟登陆

随机生成注册账号

需要使用random模块和sample方法

import random
for i in range(5):
    instance = random.sample('123abc', 3)
    print(instance)

以上实例运行后输出结果为:

['c', '2', '3']
['3', 'a', '2']
['3', 'b', 'a']
['c', '3', 'b']
['2', '1', 'a']

调整输出格式:

# 	5表示从123abc里取5个值随机排列
for i in range(5):
    instance = ''.join(random.sample('123abc', 3))+’@163.com‘
    print(instance)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

验证码的处理方法

需要安装Pillow库

  1. 使用driver.save_screenshot()截取页面图片
  2. 使用“元素”.location()方法返回元素左下角x,y坐标值
  3. 使用“元素”.size(),返回元素宽,高
  4. 使用crop((左,下,右,上))截取元素
driver.save_screenshot(r"C:\Users\douya\Desktop\shot1.png")
code_element = driver.find_element_by_id("getcode_num")
code_location = code_element.location # 返回{"x"=123,"y"=321}
left = code_element.location['x']
bottom = code_element.location['y']
code_element_size=code_element.size   #元素.size返回{'width':123,'height':
right = code_element_size['width']+left
top = code_element_size['height']+bottom
picture = Image.open(r"C:\Users\douya\Desktop\shot1.png")  #使用Image类打开图片
img = picture.crop((left,bottom,right,top))   #使用crop((左,下,右,上))裁剪图片
img.save(r"C:\Users\douya\Desktop\shot11.png")   #使用save(path)方法保存图片
driver.close()

因验证码过于复杂,可用专门打码的网站api进行打码

你可能感兴趣的:(selenium学习笔记)