Jump to content

How to get WEB form info


 Share

Recommended Posts

Hi people,

I created an script to get vendor from MAC by reading IEEE search page, thing is that I would like to show the vendor by reading the page hidding the page to the user.

Any help will be appreciated.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include<IE.au3>
#include <Array.au3>

Global $sURL = "http://standards.ieee.org/develop/regauth/oui/public.html"
Global $oIE, $oIE2,$idBtn,$MAC


GUICreate(" Vendor search", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1)
$MAC=GUICtrlCreateInput("B8-CA-3A", 10, 35, 300, 20)
$idBtn = GUICtrlCreateButton("Ok", 40, 75, 60, 20)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idBtn
            Search(GUICtrlRead($MAC))
    EndSwitch
WEnd

Func Search($MAC)
    $oIE = _IECreate($sURL, 0, 1, 1, 1)
    $num = 1
    $colForms = _IEFormGetCollection($oIE) ; get all forms
    If @error Then Return
    For $oForm In $colForms ; loop over form collection
        ConsoleWrite("---- FORM " & $oForm.name & " --------------------" & @CRLF)
        $oFormElements = _IEFormElementGetCollection($oForm) ; get all elements
        If @error Then Return
        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?)
                Switch $num
                    Case 7
                        _IEFormElementSetValue($oFormElement, $MAC, 1) ; set value of the field
                        _IEAction($oFormElement, "focus")
                        Send("{ENTER}")
                        _IELoadWait($oIE2)
                        $oTable = _IETableGetCollection($oIE, 0)
                        $aInfDisp = _IETableWriteToArray($oTable)
                        _ArrayDisplay($aInfDisp)
                EndSwitch
            EndIf
            $num += 1
        Next
    Next
EndFunc   ;==>Search
Link to comment
Share on other sites

  • Moderators

Are you using Debugbar to see the data?

There's no table, the data is in a paragraph (a string).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Smoke_N,,

I would like to get the data that appears in the second page and show it with a Message Box to the user but I don't know how to do it.

I attempted to use:

  $oTable = _IETableGetCollection($oIE, 0)
                        $aInfDisp = _IETableWriteToArray($oTable)
                        _ArrayDisplay($aInfDisp)

 

Link to comment
Share on other sites

  • Moderators

K, give me a sec, I'm rewriting your code a bit to clean it up and remove the need for it to be visible or keep creating a new IE instance.

Edit:

Okay, here's the concept I see you looking for (You'll have to clean up the return string):

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include<IE.au3>
#include <Array.au3>

Global $sURL = "http://standards.ieee.org/develop/regauth/oui/public.html"
Global $oIE, $oIE2,$idBtn,$MAC, $sReturnVal

Global $hGUI = GUICreate(" Vendor search", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1)
$MAC=GUICtrlCreateInput("B8-CA-3A", 10, 35, 300, 20)
$idBtn = GUICtrlCreateButton("Ok", 40, 75, 60, 20)
WinSetOnTop($hGUI,"", 1)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idBtn
            $sReturnVal = _SearchMAC($oIE, GUICtrlRead($MAC), $sURL)
            If Not @error Then
                MsgBox(64 + 262144, "Info", $sReturnVal)
            EndIf
    EndSwitch
WEnd

Func _SearchMAC(ByRef $oObj, $sMAC, $sURL)

    If Not IsObj($oObj) Then
        $oObj = _IECreate($sURL, 0, 0)
        _IELoadWait($oObj)
        If Not IsObj($oObj) Then
            Return SetError(1, 0, "")
        EndIf
    EndIf

    _IENavigate($oObj, $sURL)
    _IELoadWait($oObj)

    ; find the form
    Local $oForm = _IEFormGetCollection($oObj)
    If Not IsObj($oForm) Then
        Return SetError(2, 0, "")
    EndIf
    For $oForms In $oForm
        If String($oForms.className) = "bodycopy" Then
            $oForm = $oForms
            ExitLoop
        EndIf
    Next

    If Not IsObj($oForm) Then
        Return SetError(3, 0, "")
    EndIf

    ; get input
    Local $oInput = _IEGetObjByName($oForm, "x")
    If Not IsObj($oInput) Then
        Return SetError(4, 0, "")
    EndIf

    $oInput.value = $sMAC

    _IEFormSubmit($oForm)
    _IELoadWait($oObj)

    Local $oPre = _IETagNameGetCollection($oObj, "pre")
    If Not IsObj($oPre) Then
        Return SetError(5, 0, "")
    EndIf

    Return String(Execute("$oPre.item(0).innerText"))
EndFunc

You'll notice it doesn't require you to keep running new instances of IE and it doesn't require the window to be active so you can use Send().

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@jcpetu : to avoid a browser automation, you can also use the OUI database, directly with the text file provided by IEEE. (this file size is 3MB~)

Here is a example of code, using this method :

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



Local $sOui = @ScriptDir & "\oui.txt"
Local $sLink = "http://www.ieee.org/netstorage/standards/oui.txt"

GUICreate("OUI", 400, 200)
Local $ID_Mac = GUICtrlCreateInput("", 10, 10, 150, 25, $ES_UPPERCASE)
Local $ID_Search = GUICtrlCreateButton("Search", 170, 10, 100, 25)
Global $ID_Result = GUICtrlCreateEdit("", 10, 50, 380, 100, $ES_READONLY)
Local $ID_Exit = GUICtrlCreateButton("Exit", 290, 170, 100, 25)


GUISetState()

If NOT FileExists($sOui) Then
    GUICtrlSetData($ID_Result, "Loading, please wait...")
    GUICtrlSetState($ID_Mac, $GUI_DISABLE)
    GUICtrlSetState($ID_Search, $GUI_DISABLE)
    InetGet($sLink,  $sOui, 1)
    GUICtrlSetData($ID_Result, "")
    GUICtrlSetState($ID_Mac, $GUI_ENABLE)
    GUICtrlSetState($ID_Search, $GUI_ENABLE)
EndIf

Global $sOuiContent = FileRead($sOui)


While 1
    $msg = GUIGetMsg()
    If $msg = -3 OR $msg = $ID_Exit Then Exit
    
    If $msg = $ID_Search Then
        _GetMacInfo( GUICtrlRead($ID_Mac) )
    EndIf
    
WEnd


Func _GetMacInfo($sInput)
    Local $sInfo
    
    $sInput = StringRegExpReplace($sInput, "^([\dA-Fa-f]{2})[-:]([\dA-Fa-f]{2})[-:]([\dA-Fa-f]{2}).*", "$1$2$3")
    
    Local $aInfos = StringRegExp($sOuiContent, "(?s)\R\h+" & $sInput & "\h+\(base 16\)\h+(.*?)\R{2,}", 1)
    If IsArray($aInfos) Then
        $sInfo = StringRegExpReplace($aInfos[0], "(?<=[\r\n])\h+", "")
    Else
        $sInfo = "NOT FOUND"
    EndIf
    GUICtrlSetData($ID_Result, $sInfo)
EndFunc
Edited by jguinch
Link to comment
Share on other sites

  • Moderators

@jguinch

I saw that option as well, it's definitely the road I'd take (downloading the file daily probably),

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...