Sign in to follow this  
Followers 0
daddyt

Retrieving text from HTML input fields

9 posts in this topic

Hi, using the following code to parse a browser page opened up to capture various pieces of information in order to generate input for an automated testing tool, also written in autoit (which is working really well).

However, when trying to capture the input from a text input box, if it's empty, rather than getting "" returned, I seem to be getting a "0" (zero) instead.

Is that normal behaviour or am I doing something wrong? :)

The code snippet I'm running follows. It gets a collection of all objects which are input fields (radio buttons, checkboxes and text fields). Depending on the type, it output relevent details to a text control on a gui which can they be fed into the automation tool.

Thanks again for any help you can provide

$inputs = _IETagNameGetCollection($form,"input")
for  $input in $inputs
    $outval = ""
    if $input.type <> "hidden" then
        GUICtrlSetData ($Edit1, "; INPUT:" & $input.type & $crlf,1)
        select 
        case $input.type = "radio"
            if $input.checked = -1 then 
                $outval = "1"
            else
                $outval = ""
            endif
            GUICtrlSetData ($Edit1, "SR_" & $input.id & "=" & $outval & $crlf,1)
        case $input.type = "checkbox"
            if $input.checked = -1 then 
                $outval = "1"
            else
                $outval = ""
            endif
            GUICtrlSetData ($Edit1, "SR_" & $input.name & "=" & $outval & $crlf,1)
        case Else; normal text input box
        ;msgbox(0,"info",stringlen($input.value) & ":" & $input.value)
            if $input.value <> "0" then $outval = $input.value
            GUICtrlSetData ($Edit1, "SR_" & $input.name & "=" & $outval & $crlf,1)
        EndSelect
    endif
next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If the value is not specified then it returns the default, which happens to be "0" (zero).

Edit: Made an incorrect statement, see below post for correction.

Edited by big_daddy

Share this post


Link to post
Share on other sites

If the value is not specified then it returns the default, which happens to be "0" (zero).

I think I see, so the 0 isn't being returned as the value from the text box, but as a result code from trying to access the value of that box?

In that case, how could I check whether the text input field has a value or not. At the moment, I can't differentiate between an empty input box and one that has actually had a zero put in it...

Thanks again for your help ...! :)

Share this post


Link to post
Share on other sites

I think I see, so the 0 isn't being returned as the value from the text box, but as a result code from trying to access the value of that box?

In that case, how could I check whether the text input field has a value or not. At the moment, I can't differentiate between an empty input box and one that has actually had a zero put in it...

Thanks again for your help ...! :)

Use the String() function on the return value... you'll then get "" if it is empty and "0" if it is a zero.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

My previous statement was incorrect. The value property does not have a default value, so if the value is not specified then it returns "" (null).

Share this post


Link to post
Share on other sites

Hi, sorry if I'm being dense on a Friday afternoon but I must be missing something... :)

if I run this simple example, to demonstrate capturing from an empty input field

#include <IE.au3>
$ie = _IECreate("www.google.com")
$inputs = _IETagNameGetCollection($ie,"input")
for  $input in $inputs
if $input.type = "text" Then
    msgbox(0,"msgbox", "ret value:" & $input.value & "  string val:" & string($input.value))
    $input.value = "hello"
    msgbox(0,"msgbox", "ret value:" & $input.value & "  string val:" & string($input.value)); returns expected output of hello
    $input.value = ""
    msgbox(0,"msgbox", "ret value:" & $input.value & "  string val:" & string($input.value))
endif
    
Next

I don't seem to be able to get a "" from google's input box, the first and last query only ever retrieve a zero value. Is my syntax off somewhere?

Thank you

Share this post


Link to post
Share on other sites

Ah... I think I may have it now...

If I check the $input.value against chr(0) (i.e. null) then I can detect that it's empty

so my reworked example could look like this...

#include <IE.au3>

$ie = _IECreate("www.google.com")

$inputs = _IETagNameGetCollection($ie,"input")
for  $input in $inputs

if $input.type = "text" Then
    
    msgbox(0,"msgbox", "ret value:" & checkval($input))
    $input.value = "hello"
    msgbox(0,"msgbox", "ret value:" & checkval($input))
    $input.value = ""
    msgbox(0,"msgbox", "ret value:" & checkval($input))
        
endif
    
Next

func checkval($var)
    if $var.value = chr(0) then 
        $outval = "it's empty"
    Else    
        $outval = $var.value
    EndIf
    return $outval
endfunc

and it works like I hoped it would

Hopefully this will help anyone else in the same boat

Share this post


Link to post
Share on other sites

oops - sorry, in my haste to put in my reworked code, clicked wrong button before thanking you guys for your help in steering me towards the answer... :)

Thanks...

Share this post


Link to post
Share on other sites

No problem, glad you found a working solution.

Share this post


Link to post
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
Sign in to follow this  
Followers 0