AttributeError:“module”对象在python中没有属性“windll”

2024-05-15 12:10:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使一个web应用程序自动化。我应该单击一个链接,它会打开一个打印窗口。在selenium automation中,我无法实现这一点。因此,我使用ctypes来执行类似于按键的选项卡,输入键事件。下面是我为实现这一点而开发的库。我通过调用相应的方法来执行这些事件。

import ctypes
import time

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
class KeyEvents :
    def PressKey(self,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(self,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 PressAltTab(self):
        '''
        Press Alt+Tab and hold Alt key for 2 seconds in order to see the     overlay
        '''

        self.PressKey(0x012) #Alt
        self.PressKey(0x09) #Tab
        self.ReleaseKey(0x09) #~Tab
        time.sleep(2)       
        self.ReleaseKey(0x012)

    def PressTab(self):
        '''
        Press Tab Key
        '''
        #self.AltTab()          
        self.PressKey(0x09) #Tab
        self.ReleaseKey(0x09)

def PressEnter(self):
    '''
    PressEnter
    '''
    #self.AltTab()
    self.PressKey(0x0D)#Enter Key
    self.ReleaseKey(0x0D)

但是现在当我在linux中使用代码时,我得到了以下错误

File "KeyEvents.py", line 4, in <module>
SendInput = ctypes.windll.user32.SendInput
AttributeError: 'module' object has no attribute 'windll'

我不明白这里出了什么问题。我对python很陌生。请帮我一下。我应该怎么做才能在linux中实现按键事件?


Tags: selffieldsinputtimedefctypesstructuretab