Selenium+Python+Webdriver:保存截图到指定文件夹

保存图片到指定文件夹:

from selenium import webdriver
from pathlib import Path
from time import sleep
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id('kw').send_keys('selenium')
driver.find_element_by_id('su').click()
sleep(5)
# 截取当前窗口,并指定截图图片的保存位置

i=1
scrpath="C:\\Users\\whu024\\Desktop\\AmyPython"  #指定的保存目录
capturename = '\\'+str(i) + '.png'  #自定义命名截图
wholepath=scrpath+capturename
print (wholepath)
if Path(scrpath).is_dir():  #判断文件夹路径是否已经存在
    pass   
else:
    Path(scrpath).mkdir()   #如果不存在,创建文件夹
while Path(wholepath).exists():   #判断文件是否已经存在,也可使用is_file()判断
    i+=1
    capturename = '\\'+str(i) + '.png'
    wholepath = scrpath+capturename
driver.get_screenshot_as_file(wholepath) #不能接受Path类的值,只能是字符串,否则无法截图
sleep(5)
driver.quit()


保存图片到Excel:

import xlsxwriter
book = xlsxwriter.Workbook('C:\AmyPersonal\AmyMoKuai\pict.xlsx')
sheet = book.add_worksheet('demo')
sheet.insert_image('A1','0.jpg')
book.close()

 (pip install XlsxWriter)

转载于:https://www.cnblogs.com/AmyHu/p/9077431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Selenium+Pytest是一种用于自动化测试的组合。首先,需要安装PythonSelenium,以及浏览器驱动程序,如ChromeDriver或FirefoxDriver,以便使用Selenium WebDriver API来控制浏览器。\[1\]接下来,可以使用Pytest来编写和运行测试用例。 在项目结构中,通常会有一个tests文件夹,其中包含测试用例文件,如test_ui.py。还有一个pages文件夹,其中包含页面对象文件,如page_objects.py。utils文件夹中包含一些辅助函数和配置文件,如helpers.py和config.py。reports文件夹用于存储测试报告,screenshots文件夹用于存储测试过程中的,logs文件夹用于存储日志文件。drivers文件夹中包含浏览器驱动程序。还有一个requirements.txt文件用于管理项目的依赖关系,README.md文件用于提供项目的说明,run_tests.py文件用于运行测试用例。\[2\] 在终端中,可以使用pytest来运行测试。可以指定目录运行pytest test或多个目录运行pytest test test1\test11。也可以指定模块运行pytest test\test_mod.py,指定类运行pytest test\test_mod.py\Test1,指定方法运行pytest test\test_mod.py\Test1::test_01。如果需要更详细的信息,可以使用-v参数来打印测试类和测试函数的详细信息,使用-s参数来打印额外的信息。\[3\] #### 引用[.reference_title] - *1* *2* [python+selenium+pytest的UI自动设计思路](https://blog.csdn.net/xiaoxiao2235/article/details/129679022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python+selenium+pytest(中)自动化测试框架pytest的应用](https://blog.csdn.net/qq_36967200/article/details/124238560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值