python截图

截图函数通常是单独一个函数,其它函数可以调用它实现截图功能
import os,time,sys
from selenium import webdriver
def shot(curfuncname):    #定义需要传入一个参数,curfuncname为当前执行的函数名;该函数主要是为了截图取名字
  driver=webdriver.Firefox()
  driver.get("http://100.100.246.133:19143")
  driver.maximize_window()  #网页全屏
  curtime=time.strftime("%Y%m%d_%H%M%S")
  filename=curfuncname + curtime + ".jpg"  #截图的文件名称;curfuncname 为当前执行的函数名
  screenshot_path=os.path.join("/path",filename) #将保存的路径和文件名称连接起来
  driver.get_screenshot_as_file(screenshot_path) #真正的截图操作

def shot_load(): #定义该函数是为了保存截图的文件到某路径
  getcurfuncname=sys._getframe().f_code.co_name #获得当前函数的函数名
  shot(getcurfuncname)  #调用截图函数,并传入参数
shot_load()

实际截图函数的定义如下:
def getscreenshot(driver,curfuncname):  #定义函数需要传入两个参数,为什么要用参数drver,是因为截图的操作需要调用浏览器里的函数driver.get_screenshot_as_file,这里driver就相当于是浏览器的意思,因为这里没有用到driver=webdriver.Firefox()
  curtime=time.strftime("%Y%m%d_%H%M%S")
  filename=curfuncname + curtime + ".jpg"
  screenshot_path=os.path.join("/path",filename)
  driver.get_screenshot_as_file(screenshot_path)

def shot_load(): 
  driver=webdriver.Firefox()
  driver.get("http://100.100.246.133:19143")
  driver.maximize_window()
  funcname=sys._getframe().f_code.co_name
  getscreenshot(driver,funcname)

shot_load() #最后调用截图保存函数,实现完整的截图并保存

一般就放在出错的时候执行截图
#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver=webdriver.Chrome()
driver.maximize_window()
driver.get("http://xdclass.net")

login_ele=driver.find_element_by_xpath('//div[@class="login"]/span[2]')
ActionChains(driver).click(login_ele).perform()

try:
    driver.find_element_by_id('##').click()
except:
    driver.get_screenshot_as_file('login.png')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容