代码改变世界

python selenium截取指定元素图片

2018-07-11 15:26  _天枢  阅读(3424)  评论(2编辑  收藏  举报

1.截取当前屏幕

    @property
    def getImage(self):
        '''
        截取图片,并保存在images文件夹
        :return: 无
        '''
        timestrmap = time.strftime('%Y%m%d_%H.%M.%S')
        imgPath = os.path.join(gl.imgPath, '%s.png' % str(timestrmap))

        self.driver.save_screenshot(imgPath)
        print  'screenshot:', timestrmap, '.png'

 

2.截取当前元素

    def getElementImage(self,element):
        """
        截图,指定元素图片
        :param element: 元素对象
        :return: 无
        """
        """图片路径"""
        timestrmap = time.strftime('%Y%m%d_%H.%M.%S')
        imgPath = os.path.join(gl.imgPath, '%s.png' % str(timestrmap))

        """截图,获取元素坐标"""
        self.driver.save_screenshot(imgPath)
        left = element.location['x']
        top = element.location['y']
        elementWidth = left + element.size['width']
        elementHeight = top + element.size['height']

        picture = Image.open(imgPath)
        picture = picture.crop((left, top, elementWidth, elementHeight))
        timestrmap = time.strftime('%Y%m%d_%H.%M.%S')
        imgPath = os.path.join(gl.imgPath, '%s.png' % str(timestrmap))
        picture.save(imgPath)
        print  'screenshot:', timestrmap, '.png'

 

 

期待你的加入:

python|测试|技术交流 qq群:563227894

python|测试|技术交流 qq群:563227894

python|测试|技术交流 qq群:563227894