开发者

python program that plays flash games for me

Yes, that's what I need to achieve, don't ask why:) So, since this is mainly OS dependent stuff I will be using Windows or Linux (whatever is simpler) Every second my program will: 1. do a screenshot, analyze the开发者_StackOverflow社区 board and other stuff (this I can do) 2. then move the mouse to some XY and do a left-click that's all My main concern is: is there any library for capturing screenshots and then left clicking somwhere on the screen?


I've done this very thing before - use PIL to get the screenshots, and pywinauto to generate the mouse clicks.


Use ctypes and user32 calls. This is for the second part:

from ctypes import *
windll.user32.SetCursorPos(x, y)

SendInput is the thing that you're looking for to simulate mouse clicks, here's exactly what you need for clicking: http://kvance.livejournal.com/985732.html

The code for clicking is the following (tried it out, works great):

from ctypes import *
user32 = windll.user32

# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
             ("wScan", c_ushort),
             ("dwFlags", c_ulong),
             ("time", c_ulong),
             ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
             ("wParamL", c_short),
             ("wParamH", c_ushort)]

class MouseInput(Structure):
    _fields_ = [("dx", c_long),
             ("dy", c_long),
             ("mouseData", c_ulong),
             ("dwFlags", c_ulong),
             ("time",c_ulong),
             ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
              ("mi", MouseInput),
              ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
             ("ii", Input_I)]

class POINT(Structure):
    _fields_ = [("x", c_ulong),
             ("y", c_ulong)]
# END SENDINPUT TYPE DECLARATIONS

FInputs = Input * 2
extra = c_ulong(0)

click = Input_I()
click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
release = Input_I()
release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))

x = FInputs( (0, click), (0, release) )
user32.SendInput(2, pointer(x), sizeof(x[0]))


You can try to use Selenium RC + python driver for Selenium. There are means of making browser screenshot, and there is ClickAt method which takes coordinates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜