简体   繁体   English

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

[英]AttributeError: 'module' object has no attribute 'windll' in python

I am trying to automate a web application.I am supposed to click on a link and it opens an print window.I cant acheive that in selenium automation.So i used ctypes to do the keypress like tab,enter key events.Below is the library that i have developed to acheive that.I do the events by calling the respective methods. 我正在尝试使Web应用程序自动化。我应该单击一个链接并打开一个打印窗口。在硒自动化中,我无法实现这一目标。因此,我使用ctypes来执行诸如tab的按键操作,输入按键事件。下面是我已经开发出可实现此目标的库。我通过调用相应的方法来进行事件处理。

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)

But now when i use the code in linux i get the below error 但是现在当我在linux中使用代码时,出现以下错误

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

I dont understand what had gone wrong here.I am very new to python.Kindly help me out here.What should i be doing to acheive the keypress events in linux? 我不明白这里出了什么问题。我对python很陌生。请在这里帮助我。我应该怎么做才能实现linux中的按键事件?

I'm stucked at the same problem with this code too running on Raspian Jessie (Raspberry Pi). 我在Raspian Jessie(Raspberry Pi)上也运行此代码时遇到了同样的问题。 I think this is because windll.user32.SendInput is meant for Windows only. 我认为这是因为windll.user32.SendInput仅适用于Windows。 Seems like it don't have the cross platform flexibility.. 似乎没有跨平台灵活性。

If you need to do Keypress event on Linux, you can try xdotools. 如果需要在Linux上执行Keypress事件,则可以尝试xdotools。 I think it's super easy and should can fit all your needs. 我认为这非常容易,应该可以满足您的所有需求。

To install, just do 要安装,只需做

sudo apt-get install xdotool sudo apt-get安装xdotool

, and

xdotool key alt+Tab xdotool键alt + Tab

for alt+Tab key press. 按Alt + Tab键。

For more detail, you can refer to http://xmodulo.com/simulate-key-press-mouse-movement-linux.html 有关更多详细信息,您可以参考http://xmodulo.com/simulate-key-press-mouse-movement-linux.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM