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)
相关推荐
LYFlied10 分钟前
Rust代码打包为WebAssembly二进制文件详解
开发语言·前端·性能优化·rust·wasm·跨端
i建模11 分钟前
C++和Rust的性能对比
开发语言·c++·rust
盒马盒马13 分钟前
Rust:智能指针 Box & Rc & Cow
开发语言·算法·rust
极客先躯16 分钟前
Java Agent 技术全解析:从基础框架到落地实践
java·开发语言
xixixi7777719 分钟前
CDN(内容分发网络)——缓存和分发网站、应用程序、视频等内容,以提高用户访问速度和稳定性,减少网络延迟和拥塞,同时减轻源服务器的压力
网络·缓存·架构·系统架构·cdn·业务·内容分发网络
ANnianStriver23 分钟前
redis安装包方式下载安装
数据库·redis·缓存
gcbjoy28 分钟前
dbaseIII或foxplus的简化的弹出菜单实现方式
开发语言·经验分享
songroom34 分钟前
python: 扣子工作流创建与dbpystream api 自主插件开发
python
山土成旧客36 分钟前
【Python学习打卡-Day36】实战重构:用PyTorch神经网络升级信贷预测项目
python·学习·重构
姓蔡小朋友37 分钟前
LUA脚本
开发语言·junit·lua