2015-10-20 21 views
0

我正在嘗試自動化一個web應用程序。我應該點擊一個鏈接,它會打開一個打印窗口。我無法在selenium自動化中獲得成功。我使用ctypes來做按鍵,比如製表符,輸入關鍵事件。下面是我開發的用來實現這個功能的庫。我通過調用相應的方法來完成事件。AttributeError:'模塊'對象在python中沒有'windll'屬性

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.Kindly幫我在這裏。我應該做些什麼來實現linux中的按鍵事件?

回答

0

我被這個代碼運行在Raspian Jessie(Raspberry Pi)上的同樣的問題困住了。我認爲這是因爲windll.user32.SendInput僅適用於Windows。似乎它沒有跨平臺的靈活性..

如果你需要在Linux上做Keypress事件,你可以試試xdotools。 我認爲這很容易,應該可以滿足您的所有需求。

要安裝,只是做

sudo apt-get install xdotool

,並

xdotool key alt+Tab

爲ALT + TAB按鍵。

有關更多詳細信息,請參閱 http://xmodulo.com/simulate-key-press-mouse-movement-linux.html

相關問題