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)
相关推荐
hweiyu007 小时前
Redis命令:HSTRLEN
redis·缓存
怕浪猫7 小时前
RAG 面试 6 连问,从原理到优化全部覆盖
python·算法·面试
高洁018 小时前
具身智能中的世界模型训练
人工智能·python·深度学习·机器学习·transformer
无线通信科研笔记8 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
朝朝辞暮i8 小时前
VLA 系统学习第 1 课:VLA 到底在干什么?
人工智能·python·计算机视觉·vla
2601_962885729 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
爱吃奥利奥_wen9 小时前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
北冥有鱼被烹9 小时前
VCSEL全景解析:与光模块NPO CPO的关系、市场量化分析与产业链影响
python
我可能是个假开发9 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
caoerzhong9 小时前
中小企业上 WMS 该先上哪几块:JeeWMS 开源 Java 仓库管理系统的分批上线清单
java·python·开源