[英]AttributeError: 'module' object has no attribute 'windll' in python
我正在嘗試使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)
但是現在當我在linux中使用代碼時,出現以下錯誤
File "KeyEvents.py", line 4, in <module>
SendInput = ctypes.windll.user32.SendInput
AttributeError: 'module' object has no attribute 'windll'
我不明白這里出了什么問題。我對python很陌生。請在這里幫助我。我應該怎么做才能實現linux中的按鍵事件?
我在Raspian Jessie(Raspberry Pi)上也運行此代碼時遇到了同樣的問題。 我認為這是因為windll.user32.SendInput僅適用於Windows。 似乎沒有跨平台靈活性。
如果需要在Linux上執行Keypress事件,則可以嘗試xdotools。 我認為這非常容易,應該可以滿足您的所有需求。
要安裝,只需做
sudo apt-get安裝xdotool
和
xdotool鍵alt + Tab
按Alt + Tab鍵。
有關更多詳細信息,您可以參考http://xmodulo.com/simulate-key-press-mouse-movement-linux.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.