zoukankan      html  css  js  c++  java
  • 2-13 如何解决验证码代码实战

    from selenium import webdriver
    import time
    import random
    #导入PIL模块和Image:
    from PIL import Image
    #导入expected_conditions预期包判断标题是否正确:
    from selenium.webdriver.support import expected_conditions as EC
    #导入WebDriverWait
    from selenium.webdriver.support.wait import WebDriverWait
    #导入by:
    from selenium.webdriver.common.by import By
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("http://www.5itest.cn/register")
    #保存整个页面的图片用save_screenshot:
    driver.save_screenshot("C:/我的代码/selenium自动化测试/Selenium3 与 Python3 实战 Web自动化测试框架/imooc.png")
    #获取定位验证码图片元素的id:
    code_element = driver.find_element_by_id("getcode_num")
    #拿到图片的坐标值用location:
    print(code_element.location) #结果是:{"x":123,"y":345}
    #拿到左边x的值:
    left = code_element.location["x"]
    #拿到右边y的值:
    top = code_element.location["y"]
    #通过size拿到图片的宽度和高度:
    right = code_element.size["width"] + left
    height = code_element.size["height"] + top
    #打开图片:
    im = Image.open("C:/我的代码/selenium自动化测试/Selenium3 与 Python3 实战 Web自动化测试框架/imooc.png")
    #按照一定坐标裁剪图片用crop:
    img = im.crop((left,top,right,height))
    #保存图片为imooc1.png:
    img.save("C:/我的代码/selenium自动化测试/Selenium3 与 Python3 实战 Web自动化测试框架/imooc1.png")
    两张图片的结果为:

     

  • 相关阅读:
    软件测试进程&测试类型
    课堂笔记:软件测试知识点汇总小结
    闰年测试程序
    关于 int.parse("abcd") 出错的问题分析及解决方案
    软件测试——字符串检测2.0
    边界值分析法实例分析
    测试管理
    软件评审
    单元测试与集成测试
    白盒测试
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12129734.html
Copyright © 2011-2022 走看看