Python 运行imacros并拍摄所有选项卡的屏幕截图

Python 运行imacros并拍摄所有选项卡的屏幕截图,python,firefox,imacros,Python,Firefox,Imacros,我制作了一个脚本,在firefox中将imacros脚本添加到书签后触发它 该脚本运行imacro,并执行Alt+Tab(在拍摄每个选项卡的屏幕截图时更改选项卡)。执行Alt+Tab的代码来自另一个SO帖子: import subprocess import ctypes import datetime import time import os import sys import PIL import PIL.ImageGrab import win32com.client #######

我制作了一个脚本,在firefox中将imacros脚本添加到书签后触发它

该脚本运行imacro,并执行Alt+Tab(在拍摄每个选项卡的屏幕截图时更改选项卡)。执行Alt+Tab的代码来自另一个SO帖子:

import subprocess
import ctypes
import datetime
import time
import os
import sys
import PIL
import PIL.ImageGrab
import win32com.client


###################################
###################################
###################################
###################################

SendInput = ctypes.windll.user32.SendInput

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

###################################
###################################
###################################
###################################


def CtrlTab():
    '''
    Press Alt+Tab and hold Alt key for 2 seconds in order to see the overlay
    '''

    PressKey(0x11) #Ctrl
    PressKey(0x09) #Tab
    ReleaseKey(0x09) #~Tab

    time.sleep(2)       
    ReleaseKey(0x11) #~Ctrl
    time.sleep(5)   

###################################
###################################
###################################
###################################

proc = subprocess.Popen([r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', '-new-tab', 'imacros://run/?m=Demo-Firefox%5CTabs.iim'],stdin=None, stdout=None, stderr=None)
time.sleep(10)


print('a')
#CtrlTab()   

for x in list(range(0,4)):
    print('getting')
    img = PIL.ImageGrab.grab()
    print('got it')
    save_as = 'mailing_screenshots\\' + (str(datetime.datetime.now())[0:10]).replace('-','') + (str(datetime.datetime.now())[11:19]).replace(':','') + '_' + str(x) + '.jpg'

    print(save_as)          

    img.save(save_as)



    CtrlTab()
这很好,但问题是,如果我之前没有打开firefox,它将不会执行Alt+Tab。如果firefox以前(手动)打开过,Alt+Tab将起作用

关于我应该改变什么有什么想法吗?唯一的问题是,如果firefox完全关闭,选项卡将不会通过Alt+tab切换

还有别的方法吗

谢谢