首页 > 其他 > 详细

元素截图+图片对比

时间:2019-08-08 20:03:10      阅读:127      评论:0      收藏:0      [点我收藏+]

     在做web自动化的时候,我碰到一些input 类型的radio (单选框),虽然也是联动的,但是我无法根据页面元素判断选中和非选中,也无法通过is_selected()方法判断,所以想到用元素截屏后对比两张图片是否一样。

附上元素截屏代码:

 def find_element(self,loc):
        logger.info("正在准备查找{}元素".format(loc))
        try:
            web_object=self.driver.find_element(*loc)
            logger.info("{}元素查找成功".format(loc))
            return web_object
        except:
            logger.exception("{}元素查找失败") 

 def get_ele_sreenshot(self,loc,filename):
        logger.info("正在准备对{}元素对象进行截屏操作")
        try:
            self.find_element(loc).screenshot(os.path.join(ele_screenshot_path,filename))
            logger.info("{}元素对象截屏成功,并命名为{}".format(loc,filename))
        except:
            logger.exception("{}元素对象截屏失败".format(loc))

附上两张图片对比代码(需要安装第三方模块openc):

 def query_photo(self,filename_one,filename_other):
        logger.info("正在准备判断这张图片{}和这张图片{}是否一样".format(filename_one,filename_other))
        try:
            image1 = cv2.imread(os.path.join(ele_screenshot_path,filename_one))
            image2 = cv2.imread(os.path.join(ele_screenshot_path,filename_other))
            difference = cv2.subtract(image1, image2)
            result = not np.any(difference)
            if result == True:
                logger.info("两张图片一样")
                return  True
            else:
                logger.info("两张图片不一样")
                return False
        except:
            logger.exception("判断两张图片是否一样失败")

 

元素截图+图片对比

原文:https://www.cnblogs.com/Be-your-own-hero/p/11322728.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!