Jump to content

IE Automation - get all input fields and set values


Jfish
 Share

Recommended Posts

Hello all,

I confess I struggle mightly with all things IE automation. With that in mind, please excuse the simmplicity of this question and my poor code block example. I am trying to create a simple script that identifies all the input fields on an IE page and then sets their values. The idea will eventually be part of a gui that can be used to map the data elements to a data source to quickly fill out othewise time consuming forms. However, I need to get the basic identification of input fields and setting of values concept down first. To that end, I am messing around with the code below which does not work. The input name line throws an error that "the requested action with this object has failed.:" I am not sure why. Any assistance with giving me a push in the right direction would be very much appreciated.

#include<IE.au3>
#include <Array.au3>
$sURL="http://www.yahoo.com"
$oIE=_IECreate($sURL,0, 1, 1, 1)
$colForms = _IEFormGetCollection ($oIE)
For $oForm In $colForms
$oFormElements = _IEFormElementGetCollection ( $oForm )
        For $oFormElement In $oFormElements
            If $oFormElement.name = 'input' Then
                 _IEFormElementSetValue ( $oFormElement, "Found You", 0)
            EndIf
         Next
Next

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Hi,

Not all tags have name or type attribute therefor calling them will result in an error, just make sure you check for input tags first.

here is an example....

#include<IE.au3>


$sURL = "http://yahoo.com"
$oIE = _IECreate($sURL, 0, 1, 1, 1)

$colForms = _IEFormGetCollection($oIE) ; get all forms
For $oForm In $colForms ; loop over form collection

    ConsoleWrite("---- FORM " & $oForm.name & " --------------------" & @CRLF)
    $oFormElements = _IEFormElementGetCollection($oForm) ; get all elements
    For $oFormElement In $oFormElements ; loop over element collection

        If StringLower($oFormElement.tagName) == 'input' Then ; it is an input
            ConsoleWrite("> input." & $oFormElement.type & " " & $oFormElement.name & @CRLF)
            _IEFormElementSetValue($oFormElement, "Found You", 0) ; set value of the field
        ElseIf StringLower($oFormElement.tagName) == 'textarea' Then ; it is a textarea
            ConsoleWrite("> textarea " & $oFormElement.name & @CRLF)
            _IEFormElementSetValue($oFormElement, "Found You", 0) ; set value of the field
        EndIf

    Next

Next

Edit: added ElseIf for textarea tags and explanation

Edited by Robjong
Link to comment
Share on other sites

Thanks, I actually do have another question. I was hoping to avoid hidden fields with something like this:

and stringLower($oFormElement.type) <> "hidden"

However, I can't quite get it to work. I can avoid the submit button using a similar technique. Here is the code so far:

#include<IE.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:UsersRC01712DesktopTX BevwebDataMapper.kxf
$Form1 = GUICreate("DataMapper", 613, 140, 192, 132)
$websiteInput = GUICtrlCreateInput("", 8, 48, 593, 24)
$goButton = GUICtrlCreateButton("ID Fields", 168, 88, 241, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  case $goButton
   _idFields()
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd
Func _idFields()
$sURL = guictrlread($websiteInput)
$oIE = _IECreate($sURL, 0, 1, 1, 1)
$num=1
$colForms = _IEFormGetCollection($oIE) ; get all forms
For $oForm In $colForms ; loop over form collection
    ConsoleWrite("---- FORM " & $oForm.name & " --------------------" & @CRLF)
    $oFormElements = _IEFormElementGetCollection($oForm) ; get all elements
    For $oFormElement In $oFormElements ; loop over element collection
        If StringLower($oFormElement.tagName) == 'input' or stringLower($oFormElement.tagName) == 'textarea' and stringLower($oFormElement.type) <> "submit" and stringLower($oFormElement.type) <> "hidden" Then ; it is an input
   ConsoleWrite("> input." & $oFormElement.type & " " & $oFormElement.name & @CRLF)
            _IEFormElementSetValue($oFormElement, "Value " & $num , 0) ; set value of the field
        EndIf
$num+=1
    Next
Next
EndFunc

Any idea why I can't skip the hidden input fields?

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Hi again,

You are not using And/Or correctly, try changing this line

If StringLower($oFormElement.tagName) == 'input' Or StringLower($oFormElement.tagName) == 'textarea' And StringLower($oFormElement.type) <> "submit" And StringLower($oFormElement.type) <> "hidden" Then ; it is an input

to this

If (StringLower($oFormElement.tagName) == 'input' Or StringLower($oFormElement.tagName) == 'textarea') And StringLower($oFormElement.type) <> "submit" And StringLower($oFormElement.type <> "hidden") Then ; it is an input

See the difference?

Edit: However this might be a nicer way to do it

Edit 2: added error checking after _IEFormGetCollection and _IEFormElementGetCollection

#include <IE.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$Form1 = GUICreate("DataMapper", 613, 140, 192, 132)
$websiteInput = GUICtrlCreateInput("", 8, 48, 593, 24)
$goButton = GUICtrlCreateButton("ID Fields", 168, 88, 241, 25)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $goButton
            _idFields()
            If @error Then MsgBox(262160, "Error", "Something went wrong, wrong I tell you!")
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


Func _idFields()
    $sURL = GUICtrlRead($websiteInput)
    $oIE = _IECreate($sURL, 0, 1, 1, 1)
    $num = 1
    $colForms = _IEFormGetCollection($oIE) ; get all forms
    If @error Then Return SetError(1, @error, 0)
    For $oForm In $colForms ; loop over form collection
        ConsoleWrite("---- FORM " & $oForm.name & " --------------------" & @CRLF)
        $oFormElements = _IEFormElementGetCollection($oForm) ; get all elements
        If @error Then Return SetError(2, @error, 0)
        For $oFormElement In $oFormElements ; loop over element collection
            If StringInStr("input,textarea", $oFormElement.tagName) Then ; it is an input or textarea
                If StringInStr("button,hidden,reset,submit", $oFormElement.type) Then ContinueLoop ; skip to next entry if type is button,hidden,reset,submit (file,password?)
                ConsoleWrite("> input." & $oFormElement.type & " " & $oFormElement.name & @CRLF)
                _IEFormElementSetValue($oFormElement, "Value " & $num, 0) ; set value of the field
                $oFormElement.style.border = '2px solid yellow' ; does this answer your other question?
            EndIf
            $num += 1
        Next
    Next
EndFunc   ;==>_idFields
Edited by Robjong
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...