python - 属性错误 : 'module' object has no attribute 'windll' in python

标签 python linux python-2.7 selenium automation

我正在尝试自动化 Web 应用程序。我应该单击一个链接,它会打开一个打印窗口。我无法在 selenium 自动化中实现这一点。所以我使用 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。似乎它没有跨平台的灵 active ..

如果你需要在 Linux 上做 Keypress 事件,你可以试试 xdotools。 我认为它 super 简单,应该可以满足您的所有需求。

要安装,只需执行

sudo apt-get install xdotool

xdotool key alt+Tab

按 alt+Tab 键。

更详细的可以引用 http://xmodulo.com/simulate-key-press-mouse-movement-linux.html

关于python - 属性错误 : 'module' object has no attribute 'windll' in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33233449/

相关文章:

python-2.7 - python : "TypeError: ' Response' object has no attribute '__getitem__'

python - 从对象子类化是否与将类型定义为元类相同?

javascript - Python - 填写并提交 HTML 表单

python - 结合 Pandas 中的两个时间序列

Python:读取文件时创建各种文件对象

linux - 从 Windows 到 Linux 服务器的 FTP 上传时附加的 CRLF 标签

python - python中二维数组的初始化

c - 为什么客户端API和内核的setsockopt第一个参数的类型不同?

php - 找不到类 memcached

python - 使用 Python requests 模块下载并保存 PDF 文件