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)
相关推荐
武子康4 分钟前
Java-144 深入浅出 MongoDB BSON详解:MongoDB核心存储格式与JSON的区别与应用场景
java·开发语言·数据库·mongodb·性能优化·json·bjson
爱喝水的鱼丶13 分钟前
SAP-ABAP:SAP中的用户确认对话框:深入理解与实践POPUP_TO_CONFIRM
运维·开发语言·学习·sap·abap
风之所往_27 分钟前
使用 openpyxl 生成 excel 折线图
python·excel·openpyxl
小此方29 分钟前
C语言自定义变量类型结构体理论:从初见到精通(上)
c语言·开发语言
努力也学不会java35 分钟前
【Java并发】揭秘Lock体系 -- 深入理解ReentrantReadWriteLock
java·开发语言·python·机器学习
vxtkjzxt8881 小时前
自动化脚本矩阵运营
开发语言·php
王严培.1 小时前
7.MATLAB疑难问题诊疗的技术
开发语言·matlab·信息可视化
wjs20241 小时前
PHP MySQL 使用 ORDER BY 排序查询
开发语言
爱敲代码的TOM1 小时前
深入剖析Java通信架构下的三种IO模式2
java·开发语言·架构
weixin_445476681 小时前
从“用框架”到“控系统”———架构通用能力(模块边界、分层设计、缓存策略、事务一致性、分布式思维)
分布式·缓存·架构