screenscraping

Kevin Altis altis at semi-retired.com
Fri Jun 1 12:58:35 EDT 2001


The examples below are converted from MSDN VBScript examples to work with
Python on Windows (ActiveState Python (build 210) is what I tested with). It
sounds like you are wanting to do SendKeys, so you'll need to launch the app
you want to control, switch to it, and do your data entry. If the user
starts typing or clicking while the script is running, then you are probably
out of luck. Also, you should make sure to use ample delays to avoid events
getting "lost". You can use COM in many cases to do safer app control, but
it depends on the COM capabilities of the app you are interested in.

ka

--- example 1
# Python version of MSDN example for SendKeys
# uses the Windows calculator and sends keystrokes to execute a simple
calculation
#
http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/w
sMthSendKeys.htm
# the above URL also documents the codes to use with SendKeys

import win32api
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("calc")
win32api.Sleep(100)
shell.AppActivate("Calculator")
win32api.Sleep(100)
shell.SendKeys("1{+}")
win32api.Sleep(500)
shell.SendKeys("2")
win32api.Sleep(500)
shell.SendKeys("~") # ~ is the same as {ENTER}
win32api.Sleep(500)
shell.SendKeys("*3")
win32api.Sleep(500)
shell.SendKeys("~")
win32api.Sleep(2500)

--- example 2
# various Windows Script Host (WSH) examples converted to Python
# converted from
#
http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/w
sMthRun.htm
# http://msdn.microsoft.com/scripting/windowshost/doc/wsproenvironment.htm
#
http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/w
sMthSendKeys.htm

import sys
import win32api
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
# launch Notepad to edit this script, simple version
# sys.argv[0] is WScript.ScriptFullName in WSH
#shell.Run("notepad " + sys.argv[0])

# this time set the window type, wait for Notepad to be shut down by the
user,
# and save the error code returned from Notepad when it is shut down
# before proceeding
ret = shell.Run("notepad " + sys.argv[0], 1, 1)
print ret


"Paul Brian" <pbrian at demon.net> wrote in message
news:991411356.20275.0.nnrp-10.c1c3e154 at news.demon.co.uk...
> all,
>
> I may be scraping the bottom of the barrel as well a screen, but I would
> like to know if it is possible to (using python) activate a windows
> application that takes input from a GUI, and enter in data through the GUI
> programmatically.
> For example, bring up the address page, "tab" over to Surname and enter in
> "foo"
> I am prevented from using any kind of API or direct interface to real
work.
>
> Thank you
>
>
> P Brian
>
>
>





More information about the Python-list mailing list