Jump to content

Input form with javascript option


Recommended Posts

I am newbie on the forum. I have been referring to the forums to get any help on autoit.

I have used _IEFormGetObjByName,_IEFormElementGetObjByName,_IEFormElementSetValue to input values in a form.

In the particular situation presented below, I have selected the option using _IEFormElementOptionselect but could not setvalue to the "value" of the option.I have tried many functions such as _IEFrameGetCollection,_IEGetObjById,_IETagNameGetCollection but could not succeed in setting value to the option

The source code is as follows:

<option value='ESRB'>ESRB</option>

<input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" />

I would like to setvalue to the 'ESRB' option and would like to get help out here in solving the trouble I have been facing since two days.

Thanks

Link to comment
Share on other sites

I don't think it's proper HTML to have that input tag inside the select/option tags, but this works anyway:

#include <IE.au3>

$sHtml = '<html>' & @CRLF & _
        '   <body>' & @CRLF & _
        '       <form action="">' & @CRLF & _
        '           <select name="selections">' & @CRLF & _
        '               <option value="ABCD">ABCD</option>' & @CRLF & _
        '               <option value="EFGH">EFGH</option>' & @CRLF & _
        '               <option value="IJKL" selected="selected">IJKL</option>' & @CRLF & _
        '               <option value="MNOP">MNOP</option>' & @CRLF & _
        '               <option value="ESRB">ESRB</option>' & @CRLF & _
        '               <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" /> ' & @CRLF & _
        '           </select>' & @CRLF & _
        '       </form>' & @CRLF & _
        '   </body>' & @CRLF & _
        '</html>'

$oIE = _IECreate()
_IEDocWriteHTML($oIE, $sHtml)

Sleep(2000)

$oForm = _IEFormGetCollection($oIE, 0) ; Get first form by index
$oSelect = _IEFormElementGetObjByName($oForm, "selections")
_IEFormElementOptionselect($oSelect, "ESRB")

Sleep(2000)

$oInput = _IEFormElementGetObjByName($oForm, "metakeyinput")
_IEFormElementSetValue($oInput, "ESRB Be Me!")

But maybe I don't understand what you are trying to do.

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Let me present you the entire code for clarity

<div id="postcustom" class="postbox " >

<div class="handlediv" title="Click to toggle"><br /></div><h3 class='hndle'><span>Custom Fields</span></h3>

<div class="inside">

<div id="postcustomstuff">

<div id="ajax-response"></div>

<table id="list-table" style="display: none;">

<thead>

<tr>

<th class="left">Name</th>

<th>Value</th>

</tr>

</thead>

<tbody id="the-list" class="list:meta">

<tr><td></td></tr>

</tbody>

</table><p><strong>Add New Custom Field:</strong></p>

<table id="newmeta">

<thead>

<tr>

<th class="left"><label for="metakeyselect">Name</label></th>

<th><label for="metavalue">Value</label></th>

</tr>

</thead>

<tbody>

<tr>

<td id="newmetaleft" class="left">

<select id="metakeyselect" name="metakeyselect" tabindex="7">

<option value="#NONE#">&mdash; Select &mdash;</option>

<option value='ESRB'>ESRB</option>

<option value='Publisher'>Publisher</option>

<option value='Release Date'>Release Date</option>

<option value='Thumbnail'>Thumbnail</option></select>

<input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" />

<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">

<span id="enternew">Enter new</span>

<span id="cancelnew" class="hidden">Cancel</span></a>

</td>

<td><textarea id="metavalue" name="metavalue" rows="2" cols="25" tabindex="8"></textarea></td>

</tr>

<tr><td colspan="2" class="submit">

<input type="submit" id="addmetasub" name="addmeta" class="add:the-list:newmeta" tabindex="9" value="Add Custom Field" />

<input type="hidden" id="_ajax_nonce-add-meta" name="_ajax_nonce-add-meta" value="0e681fd27c" /></td></tr>

</tbody>

</table>

</div>

I would like to setvalue to the option field when the option 'ESRB' is selected.

Thanks

Link to comment
Share on other sites

What "option field" do you want to change? Modified demo:

#include <IE.au3>

$sHtml = '<html>' & @CRLF & _
        '   <body>' & @CRLF & _
        '       <form action="">' & @CRLF & _
        '           <select id="metakeyselect" name="metakeyselect" tabindex="7">' & @CRLF & _
        '               <option value="#NONE#">&mdash; Select &mdash;</option>' & @CRLF & _
        '               <option value="ESRB">ESRB</option>' & @CRLF & _
        '               <option value="Publisher">Publisher</option>' & @CRLF & _
        '               <option value="Release Date">Release Date</option>' & @CRLF & _
        '               <option value="Thumbnail">Thumbnail</option>' & @CRLF & _
        '           </select>' & @CRLF & _
        '           <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" /> ' & @CRLF & _
        '       </form>' & @CRLF & _
        '   </body>' & @CRLF & _
        '</html>'

$oIE = _IECreate()
_IEDocWriteHTML($oIE, $sHtml)
$oForm = _IEFormGetCollection($oIE, 0) ; Get first form by index
$oSelect = _IEFormElementGetObjByName($oForm, "metakeyselect")

Do
    Sleep(100)
    $sValue = _IEFormElementGetValue($oSelect)
Until $sValue = "ESRB"

ConsoleWrite("$sValue = " & $sValue & @LF)
$oInput = _IEFormElementGetObjByName($oForm, "metakeyinput")
_IEFormElementSetValue($oInput, "ESRB Selected!")

:blink:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the quick reply.

Sorry,I could not get the results from the code.

I am attaching an image of the form.

I intend to autosubmit the form by setting values to the fields of the form.

From the image, you could see the "Add New custom Field:", there is a dropdown menu where there are four options namely: ESRB,Publisher,Release Date,Thumbnail.

I need to select the option from the dropdownmenu and submit its value in the text box placed to the right of the dropdownmenu in header named "value".

Thanks for you regard to newbies on the forum with quick replies.post-59058-12792218062731_thumb.jpg

P.S :The source code of the form is provided in the previous post

Link to comment
Share on other sites

Well, you still didn't post all of the html, so we can't tell if there's a Form, Frame, or iFrame to deal with, but this is still the general idea:

$oForm = _IEFormGetCollection($oIE, 0) ; Get first form by index
$oSelect = _IEFormElementGetObjByName($oForm, "metakeyselect")

Do
    Sleep(100)
    $sValue = _IEFormElementGetValue($oSelect)
Until $sValue <> "#NONE#"

$oInput = _IEFormElementGetObjByName($oForm, "metakeyinput")
_IEFormElementSetValue($oInput, $sValue)

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

It is running into a infinite loop with an error

--> IE.au3 V2.4-0 Error from function _IEFormElementGetValue, $_IEStatus_InvalidDataType.

Do you mind if I PM the screenshot of the form along with the entire source code.

I went through a post that suggests me to solve in a different way.

http://www.autoitscript.com/forum/index.php?showtopic=27212

Does the post help me in any way in setting value of the optionfield.

Edited by jones
Link to comment
Share on other sites

I didn't expect the code I posted to work literally with your form. It just illustrates the technique. Probably _IEFormElementGetValue() fails because _IEFormElementGetObjByName() didn't succeed in getting $oSelect. The most likely cause of that is $oForm not being correct, because _IEFormGetCollection($oIE, 0) failed or got the wrong form.

You have to do the following:

1. Get a valid reference to IE in $oIE, either by _IECreate() or by _IEAttach()

2. Get a valid reference to the form, which may mean drilling down into a parent form or frame

3. Get a valid reference to "metakeyselect" select element.

4. Get a valid reference to "metakeyinput" input element.

How to do that depends on the particular web page you are working with. Time to bust out the help file and learn to use the functions.

The following tips will help:

A. Add _IEErrorHandlerRegister() to the top of the script.

B. Run your script from SciTE so the error messages are easy to read in the console pane.

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...