Author Topic: "Search function" to locate a button on a web page  (Read 2632 times)

Birgit

  • Newbie
  • *
  • Posts: 1
"Search function" to locate a button on a web page
« on: January 23, 2014, 04:12:12 am »
Hi,
I need to automate the interaction with a web page.
Whenever possible I use shortcuts, but I need to press a specific button at one point. I am using the "mousemove" function, but that only works for 80% of the tasks, because the button is not always to be found at the same coordinates.
I would prefer to use a "search" function in order to locate the button, but I don't know if that is possible and how to do it (a simple text search does not work.)
Thank you for any andvice.
Best Regards
Birgit

stanl

  • Pundit
  • *****
  • Posts: 951
Re: "Search function" to locate a button on a web page
« Reply #1 on: January 23, 2014, 07:53:37 am »
How about DHTML? There is an excellent tutorial in the Tech DB about automating web pages:

http://techsupt.winbatch.com/webcgi/webbatch.exe?Techsupt/tsleft.web+Tutorials+Working~With~Web~Pages.txt


Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: "Search function" to locate a button on a web page
« Reply #2 on: January 23, 2014, 09:11:40 am »
What browser is running? If you are working with Internet Explorer then the webpage elements are accessible using COM automation. Here is a sample script that can help get you started in discovering various web page elements:

Code: Winbatch
;***************************************************************************
;**        Webpage Element Explorer
;**
;** Purpose: Discovering form elements on a webpage
;** Inputs: ignorehidden, inputonly
;** Outputs: Highlights each form element using a red solid border
;**          and Display attribute information in a Box
;** Reference:
;**        
;**
;** Developer: Deana Falk 2013.10.24
;***************************************************************************

#DefineFunction udfIEPageLoadWait( objIE )
    ; Wait for webpage to load
    While !(objIE.readyState == 'complete' || objIE.readyState == 4 )
       Timedelay(0.1)            
    EndWhile
    While !(objIE.document.readyState == 'complete' || objIE.document.readyState == 4 )
       Timedelay(0.1)
    EndWhile
    Return 1
#EndFunction

; Modify these values to change how the script operates
ignorehidden = @TRUE       ; change to @FALSE to query hidden form elements
inputonly = @FALSE         ; change to @TRUE to query only INPUT form elements.
boxcoords =  '750,0,1000,200' ; coordinates of the display box.
boxclr =  '255,0,0'  ; box background color
url = "http://techsupt.winbatch.com/techsupt/sampleform.html"


; Display form element data in a box
title = 'Discovering form elements on a webpage'
BoxesUp(boxcoords, @NORMAL)
BoxDrawRect(1,'0,0,1000,1000',2)
BoxColor(1,boxclr,0)
BoxCaption(1, title)
WindowOnTop(title, 1)


; Initialize MSIE object
oIE   = ObjectCreate("InternetExplorer.Application")
oIE.Visible = @TRUE ; Change to @FALSE to hide the process from the user
oIE.Navigate(url)

; Wait for webpage to load
udfIEPageLoadWait( oIE )

;***************************************************************************
; Get Colection of Forms
;***************************************************************************
; Get document object
oDoc = oIE.Document

; Forms Collection  
oForms = oDoc.Forms
; OR
; getElementsByTagName Method
;oForms = oDoc.getElementsByTagName("FORM")

formlist = "" ; Initialize variable
If oDoc != 0 ; Check if you have a valid document handle
   ; Loop through the collection of forms using index
   count = oForms.Length
   For index = 0 to count-1
         oForm = oForms.Item(index)
         If ObjectTypeGet(oForm)=="EMPTY" then continue
         formlist = formlist:@tab:index:"|":oForm.id:"|":oForm.name
   Next  
   ; ALTERNATE WAY: Loop through the collection of forms
   ;index = 0
   ;ForEach oForm in oForms
   ;   If ObjectTypeGet(oForm)=="EMPTY" then continue  
   ;   formlist = formlist:@tab:index:"|":oForm.id:"|":oForm.name
   ;   index = index+1
   ;Next
Else
   Pause('Notice','Unable to obtain a handle to the document object on this webpage. Check URL.')
EndIf
formlist = StrTrim(formlist) ; remove leading tab
selection = ""
If formlist != ""
   While selection == ""
      selection = AskItemlist("Select Form Index on a webpage", formlist, @tab, @unsorted, @single )
   Endwhile
   formindex = Int(ItemExtract(1,selection,"|"))
   oForm = oForms.Item(formindex)
   ;Pause('Form Object Handle ', oForm)
Else
   Pause('Notice','There are no forms on this page!')
EndIf

;***************************************************************************
; Get Collection of Elements in the Form
;***************************************************************************
If oForm != 0 ; Check if you have a valid form handle
   cElements = oForm.Elements
   elementindex = 0
   BoxButtonDraw(1, 1, 'Next Element', '250,750,650,900')
   ForEach oElement in cElements
      ; Get element attributes
      ; Alternatice option: oElement.getAttribute("attributename")
      tag = oElement.nodeName ;The value of tagName is the same as that of nodeName.
      type = StrUpper(oElement.Type)
      id = oElement.Id
      name = oElement.Name
      value = oElement.Value  

      ; Check if user wants to ignore HIDDEN form elements
      If ignorehidden
          If StrUpper(type) == 'HIDDEN' then continue
      EndIf

      ; Check if user wants to see non INPUT form elements
      If inputonly
         If StrUpper(tag) != 'INPUT' then continue
      EndIf

      ; Highlight each element on the form
      oElement.style.border = "1mm solid red"
     
      ; Set focus on element
      If !oElement.disabled
         ErrorMode(@off)
         oElement.Focus()
         ErrorMode(@cancel)
      EndIf
     
      ; Display results
      BoxDrawText(1, "0,0,1000,1000",'Element Index: ' : elementindex : @lf :'Tag: ' : tag : @lf : 'Type: ' : type : @lf :'Id: ' : id : @lf :'Name: ' : name : @lf :'Value: ': value, @TRUE, 0 )
      While !BoxButtonStat(1, 1)
         TimeDelay(0.5)
      Endwhile  

      ; Remove element highlight
      oElement.style.border = ""

      ; Increment element counter
      elementindex = elementindex+1
   Next
Else
   Pause('Notice','Unable to obtain a handle to a form on this webpage. Check URL and formnumber.')
EndIf

:CleanUp

; Quit
oIE.Quit

; Close open COM/OLE handles
oElement
oDoc = 0
oIE = 0
exit


Deana F.
Technical Support
Wilson WindowWare Inc.