Selenium网页长截图

一,背景

复制代码
1,在实际业务中,执行自动化任务时,会将执行结果截图并返回,但是selenium原生不支持长截图。

二,解决

复制代码
先将网页缩小后,将网页分割成两份进行截图然后拼接。
python 复制代码
    def screenshotByElement(self):
        
        try:
        	"""首先生成最终结果图文件"""
            self.screenshotPicName = self.screenshotPicName_tail = f"C:\\tools\\02201\\{datetime.datetime.now().strftime('%Y-%m-%d')}\\{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}_{self.bookingreq_id}.png"
            self.driver.get_screenshot_as_file(self.screenshotPicName)
            
			"""将网页缩小"""
            self.driver.execute_script("document.body.style.zoom = 0.5;")
            actions = ActionChains(self.driver)
            """将鼠标焦点移动至主元素"""
            div_element = self.driver.find_element(By.ID, 'app')
            actions.move_to_element(div_element).perform()

            """将页面拉至最顶端"""
            actions.key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform()
            actions.move_to_element(div_element).perform()
            actions.key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform()
            actions.move_to_element(div_element).perform()
            actions.key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform()
            actions.move_to_element(div_element).perform()
            actions.key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform()

            sleep(0.5)
            """截图第一张"""
            self.screenshotPicName_top = f"C:\\tools\\02201\\{datetime.datetime.now().strftime('%Y-%m-%d')}\\{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}_{self.bookingreq_id}_top.png"
            self.driver.get_screenshot_as_file(self.screenshotPicName_top)

			"""算好位置   向下拉动  """
            actions.move_to_element(div_element).perform()
            actions.key_down(Keys.PAGE_DOWN).key_up(Keys.PAGE_DOWN).perform()

            sleep(0.5)
            """算好位置   截图第二张  """
            self.screenshotPicName_tail = f"C:\\tools\\02201\\{datetime.datetime.now().strftime('%Y-%m-%d')}\\{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}_{self.bookingreq_id}_tail.png"
            self.driver.get_screenshot_as_file(self.screenshotPicName_tail)

            """拼接图片"""
            self.joinPic(self.screenshotPicName_top,self.screenshotPicName_tail)
        except Exception as e:
            self.logger.info(str(e))
            self.screenshotPicName=None

    def joinPic(self,pic1,pic2):
        """将两张图片上下  拼接在一起"""
        image1 = Image.open(pic1)
        image2 = Image.open(pic2)
        new_image = Image.new('RGB', (image1.width, image1.height + image2.height))
        new_image.paste(image1, (0, 0))
        new_image.paste(image2, (0, image1.height))
        new_image.save(self.screenshotPicName)
相关推荐
jinanwuhuaguo11 分钟前
人工智能的进化阶梯:AI、ANI、AGI与ASI的核心区别与深度剖析
开发语言·人工智能·agi·openclaw
清空mega18 分钟前
C++中关于数学的一些语法回忆(2)
开发语言·c++·算法
Mr_Xuhhh39 分钟前
从理论到实践:深入理解算法的时间与空间复杂度
java·开发语言·算法
一个有温度的技术博主41 分钟前
Redis AOF持久化:用“记账”的方式守护数据安全
redis·分布式·缓存
Lenyiin1 小时前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
涛声依旧393161 小时前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘
企鹅的蚂蚁2 小时前
【ESP32-S3开发踩坑】C++野指针引发的LoadProhibited死机与CMake依赖锁死排查
开发语言·c++
kcuwu.2 小时前
Python进阶:生成器与协程,高效并发编程的核心实践
windows·python·php
XiaoQiao6669992 小时前
python 简单题目练手【详解版】【1】
开发语言·python
Kiling_07042 小时前
Java多态、final与抽象类:面向对象编程进阶指南
java·开发语言