This is an tool for automating the browser via shellwindows. This class file will allow a user to find any "OPEN" browser window and automate it.
You can access an "open" browser window, set the text in the Textbox, Select a List item or Click a forms submit button.
enjoy RLM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | """
WSA - browser tool
Author: Robert Marchetti
Site: http://pamie.sourceforge.net
Date: July 1, 2004
Description: Allows user to find any "OPEN" browser window and automate it.
I make no warranties for cWSA.py
Tested against XP and Win2000 with Python 2.34 and Win32All
Use at your own risk!
"""
import win32com.client
import time
class WSA:
def __init__(self):
""" Constructor """
clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
## Set new instance of the shellwindow as sw
self._sw = win32com.client.Dispatch(clsid)
""" Create new instance """
print 'Window Found:',self._sw
def Navigate(self,wintext,url):
""" This function Navigates to the url
parameters:
wintext - title of the window
url - url to go to
"""
self._ie.Navigate(url)
def GetElemNames(self, wintext,formname):
""" This function gets the element names from the form
parameters:
wintext - title of the window
winnum - url to go to
formname - Name or number of form
"""
for i in range(self._sw.Count): ## Count windows
if i is None:
print 'No Open Windows Found'
break
time.sleep(.2) ## may have to play with this value
print i
wn = self._sw[i].LocationName ## Get the window Title
print wn
## checks to see if location matches the name
if wn == wintext:
form = self._sw[i].Document.forms[formname]
count = 0
while count < form.elements.length:
elements = form.elements[count]
count +=1
name = elements.name
print 'Following elements found: ',name
# Works for List box also
def SetText(self, wintext,element,value,frmname): #Win Title as Param
""" This function sets the text in a textbox """
for i in range(self._sw.Count): ## Count windows
if i is None:
print 'No Open Windows Found'
break
time.sleep(.2) ## may have to play with this value
print i
wn = self._sw[i].LocationName ## Get the window Title
print wn
## checks to see if location matches the name
if wn == wintext:
print "sw[", i, "]" ## print the win number
win = self._sw[i].LocationName ## Get the window Title
print "Window name: ", win
try:
self._sw[i].Document.forms[frmname].elements[element].value= value
print 'textbox value set to: ', value
except:
print 'SetText Failed'
def CloseWin(self, wintext):
""" This function closes a window"""
for i in range(self._sw.Count): ## Count windows
if i is None:
print 'No Open Windows Found'
break
time.sleep(.2) ## may have to play with this value
wn = self._sw[i].LocationName ## Get the window Title
## checks to see if location matches the name
if wn == wintext:
try:
self._sw[i].quit()
print "Closed Window"
except:
print 'CloseWin failed to close the window'
def ClickButton(self,wintext,element,frmname): #Win Title as Param
""" This function clicks a button"""
for i in range(self._sw.Count): ## Count windows
if i is None:
print 'No Open Windows Found'
break
time.sleep(.2) ## may have to play with this value
wn = self._sw[i].LocationName ## Get the window Title
## checks to see if location matches the name
if wn == wintext:
print "sw[", i, "]" ## print the win number
win = self._sw[i].LocationName ## Get the window Title
print "Window name: ", win
##Place TestCode goes below
try:
self._sw[i].Document.forms[frmname].elements[element].click()
except:
print 'ClickButton Failed'
def SetListbox(self, wintext,element,value,frmname): #Win Title as Param
""" This function select an item from a listbox"""
for i in range(self._sw.Count): ## Count windows
if i is None:
print 'No Open Windows Found'
break
time.sleep(.2) ## may have to play with this value
wn = self._sw[i].LocationName ## Get the window Title
print wn
## checks to see if location matches the name
if wn == wintext:
print "sw[", i, "]" ## print the win number
win = self._sw[i].LocationName ## Get the window Title
print "Window name: ", win
try:
self._sw[i].Document.forms[frmname].elements[element].value = value
print 'Selected item: ', value
except:
print 'SetListBox failed'
# -------------End of Method calls-------------------
if __name__ == "__main__":
# Start test
print 'starting test'
|
Tags: web
Connecting to running instances of IE on your computer. Did you see this sample on the cookbook done using raw ctypes? http://aspn.activestate.com/ASPN/Cookbook/PHP/Recipe/302089 Eric
Is it possible to put a sample test after if __name__ == "__main__": on how to use it?
Eric
RE: Connecting to running instances of IE on your computer. No, but thanks for pointing me to it! I have another program Pamie that controls and instance of a browser so my example is just bacially using the same code.
http://pamie.sourceforge.net
Is it possible to put a sample test after if __name__ == "__main__": I know, the rest of the code for the test got cut off. Unfortunately I never went back to fix it.
Thanks Rob
RE: Connecting to running instances of IE on your computer, I figured this example would be useful for those that are involved in browser testing.