python selenium截取指定元素圖片


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM