GerryIT

Help selecting item in dropdown box

16 posts in this topic

Hi,

I'm trying to select an item in a dropdown box but when I submit the form, seems like the item is not selected although it appeared in the box.

Here is the html code of the form:

<input type="hidden" id="pstad-loctnid" name="postAdForm.locationId" value="80002"/>
    <li>
        <a name="postAdForm.location"></a>
        <label for="locationLevel0" class="add-asterisk">Ville&nbsp;:</label>
        <div class="form-section">
            <select id="locationLevel0" name="locationLevel0" req="req" class="locationSelector ">
                <option value="">----------</option>
                <option value="1700278">
                    Laval / Rive-Nord</option>
                <option value="1700279">
                    Longueuil / Rive-Sud</option>
                <option value="1700280">
                    Ouest de l'île</option>
                <option value="1700281">
                    Ville de Montréal</option>
            </select>
            <div class="field-message" data-for="locationLevel0"></div>
            <p class="message">Veuillez sélectionner votre emplacement ou un lieu près de chez vous.</p>
        </div>
    </li>
;

And here is the script code I'm using:

Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$selects = $oIEBody.GetElementsByTagname("select")
$numofselects = $selects.length
For $r = 0 To $numofinputs - 1
    $myobj = $selects.item($r)
    If $myobj.id = "locationLevel0" And $myobj.name="locationLevel0" Then
        _IEAction($myobj, "focus")
        $myobj.value = "1700281"
        ExitLoop
    EndIf
Next
;

The "Ville de Montréal" option appears in the box but when I submit the form, I get a message that it's missing this information.

Please tell me what I'm doing wrong.

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try putting $myobj.value = "1700281" before the "focus" and changing "focus" to "click".

If that doesn't work add a "click" after the "focus"

Edited by Dent

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Try this:

#include <IE.au3>

; REMARK:
; Before you run this script you should copy HTML snippet to Clipboard

_Example()
Func _Example()
    Local $oIE = _IECreate()
    _IEDocWriteHTML($oIE, ClipGet())

    Local $oIE_Select = _IEGetObjById($oIE, 'locationLevel0')
    _IEFormElementOptionSelect($oIE_Select, '1700281', 1, "byValue")
EndFunc   ;==>_Example

 

Edited by mLipok

Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

Sorry mLipok,

Tried you solution, it does the same as my original scrtipt.

It does selects the  "Ville de Montréal" option that appears in the box but when I submit the form, I get a message that it's missing this information again.

Will try Dent's solution...

Share this post


Link to post
Share on other sites
_IEFormElementOptionSelect($oIE_Select, '1700281', 1, "byValue", 0)

?


Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

Sorry mLipok,

Tried you solution, it does the same as my original scrtipt.

It does selects the  "Ville de Montréal" option that appears in the box but when I submit the form, I get a message that it's missing this information again.

Will try Dent's solution...

1-Open_New_form.jpg

2-After_Script.jpg

3-After_Submit.jpg

4-Dropdown_List.jpg

Share this post


Link to post
Share on other sites

Tried adding the ", 0" at the end of the line and it does the same thing.

Added the form's photos before, during and after submit...

Share this post


Link to post
Share on other sites

Dent, sorry but it does the same as my original script.

It does changes the selection box from "--------" to "Ville de Montréal" but we I click submit, the form returns with the same filled form and a top section

showing a message stating to correct errors on the page and lower it says "You have not finished filling the form".

5-Error_Msg.jpg

Share this post


Link to post
Share on other sites

Can you share the URL or all the HTML? Remeber to post the code in tags <> (just click in this symbol <> when posting).

Share this post


Link to post
Share on other sites

After the submit and the return of the form with the error message, the dropdown selection box is back to "--------" as if nothing was ever selected.

 

Share this post


Link to post
Share on other sites

An here is the full script to test the form.

While the script starts entering fields, scroll down mid page to see the city selection of "Ville de Montréal".

After the script ends, click on the button: "Afficher votre annonce".

You should get the page coming back filled with the error and the city being back to "--------".

Share this post


Link to post
Share on other sites

Oops, here's bthe code:

;======================================================================================
#include <IE.au3>
#include <MsgBoxConstants.au3>
Global $oIE = _IECreate ("http://www.kijiji.ca/h-grand-montreal/80002", 0, 1, 1)
Opt ("SendKeyDelay", 100)
Local $hWnd = WinWait($oIE, "", 5) ; Wait 5 seconds for the window to appear.1
WinSetState(_IEPropertyGet($oIE, "hwnd"), "", @SW_MAXIMIZE)
;======================================================================================
; AFFICHER UNE ANNONCE
;======================================================================================
Sleep(1000)
$oIEDoc  = $oIE.Document
$oIEBody = $oIEDoc.body
$oDiv1 = _IEGetObjById($oIE, "PostAdLink")
_IEAction($oDiv1, "click")
;======================================================================================
; Catégorie CHAMBRES À LOUER, COLOCS
;======================================================================================
Sleep(1000)
$oIEDoc  = $oIE.Document
$oIEBody = $oIEDoc.body
$oDiv2 = _IEGetObjById($oIE, "CategoryId37")
_IEAction($oDiv2, "click")
;======================================================================================
; Nombre de pièces
;======================================================================================
Sleep(1000)
$oIEDoc  = $oIE.Document
$oIEBody = $oIEDoc.body
_IELinkClickByText($oIE, "4 1/2")
;======================================================================================
; PRIX
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "priceAmount" And $myobj.type="text" Then
        $myobj.value="100.00"
        ExitLoop
    EndIf
Next
;======================================================================================
; Propriéraire
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "forrentbyhousing_s" And $myobj.value="ownr" Then
        _IEAction($myobj, "click")
        ExitLoop
    EndIf
Next
;======================================================================================
; Nombre de salles de bain
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$selects = $oIEBody.GetElementsByTagname("select")
$numofselects = $selects.length
For $r = 0 To $numofinputs - 1
    $myobj = $selects.item($r)
    If $myobj.name = "postAdForm.attributeMap[numberbathrooms_s]" And $myobj.id="numberbathrooms_s" Then
        $myobj.value = "10"
        ExitLoop
    EndIf
Next
;======================================================================================
; Meublé
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "furnished_s" And $myobj.value="1" Then
        _IEAction($myobj, "click")
        ExitLoop
    EndIf
Next
;======================================================================================
; Animaux
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "petsallowed_s" And $myobj.value="0" Then
        _IEAction($myobj, "click")
        ExitLoop
    EndIf
Next
;======================================================================================
; Titre
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "postad-title" And $myobj.type="text" Then
        $myobj.value="Test-01"
        ExitLoop
    EndIf
Next
;======================================================================================
; Description
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$textareas = $oIEBody.GetElementsByTagname("textarea")
$numoftextareas = $inputs.length
For $r = 0 To $numoftextareas - 1
    $myobj = $textareas.item($r)
    If $myobj.id = "pstad-descrptn" And $myobj.name="postAdForm.description" Then
        $myobj.value="Test-02 with minimum 10 chars"
        ExitLoop
    EndIf
Next
;======================================================================================
; Code postal
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "addressPostalCode" And $myobj.type="text" Then
        $myobj.value="H1A1A1"
        ExitLoop
    EndIf
Next
;======================================================================================
; Lieu
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "AddressCity" And $myobj.type="text" Then
        $myobj.value="Test-03"
        ExitLoop
    EndIf
Next
;======================================================================================
; Ville
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$selects = $oIEBody.GetElementsByTagname("select")
$numofselects = $selects.length
For $r = 0 To $numofinputs - 1
    $myobj = $selects.item($r)
    If $myobj.id = "locationLevel0" And $myobj.name="locationLevel0" Then
        $myobj.value = "1700281"
        _IEAction($myobj, "focus")
        $myobj.value = "1700281"
        ExitLoop
    EndIf
Next
;======================================================================================
; Téléphone
;======================================================================================
Sleep(1000)
$oIEDoc = $oIE.Document
$oIEBody = $oIEDoc.body
$inputs = $oIEBody.GetElementsByTagname("input")
$numofinputs = $inputs.length
For $r = 0 To $numofinputs - 1
    $myobj = $inputs.item($r)
    If $myobj.id = "PhoneNumber" And $myobj.type="text" Then
        $myobj.value="555-555-5555"
        ExitLoop
    EndIf
Next
;======================================================================================

 

Share this post


Link to post
Share on other sites

I made some changes. After i click in the button "Afficher votre annonce" its giving me another error "Une erreur est survenue lors de l'affichage de votre annonce. Veuillez contact le Service à la clientèle." probably because we are filling the form with "test" data. Try to fill with the correct data to see if its correct.

;======================================================================================
#include <IE.au3>
#include <MsgBoxConstants.au3>

Local $oIE = _IECreate ("https://www.kijiji.ca/p-post-ad.html?categoryId=214")
;~ Opt ("SendKeyDelay", 100)
Local $hWnd = WinWait($oIE, "", 5) ; Wait 5 seconds for the window to appear.1
WinSetState(_IEPropertyGet($oIE, "hwnd"), "", @SW_MAXIMIZE)
;======================================================================================
; AFFICHER UNE ANNONCE
;======================================================================================
$oANNONCE = _IEGetObjById($oIE, "PagePostAsOwner")
_IEAction($oANNONCE, "click")
_IELoadWait($oIE)
Sleep(5000) ; just to make sure
;======================================================================================
; PRIX
;======================================================================================
Local $oPrix = _IEGetObjById($oIE, "priceAmount")
_IEFormElementSetValue($oPrix, "100.00")
;======================================================================================
; Propriéraire
;======================================================================================
Local $oForm = _IEFormGetObjByName($oIE, "PostAdMainForm")
_IEFormElementRadioSelect($oForm, "ownr", "forrentbyhousing_s")
;======================================================================================
; Nombre de salles de bain
;======================================================================================
Local $oN_Salles_de_bain   = _IEGetObjById($oIE, "numberbathrooms_s")
_IEFormElementOptionSelect($oN_Salles_de_bain, "10")
;======================================================================================
; Meublé
;======================================================================================
_IEFormElementRadioSelect($oForm, "1", "furnished_s")
;======================================================================================
; Animaux
;======================================================================================
_IEFormElementRadioSelect($oForm, "0", "petsallowed_s")
;======================================================================================
; Titre
;======================================================================================
Local $oTitre = _IEGetObjById($oIE, "postad-title")
_IEFormElementSetValue($oTitre, "Test-01")
;======================================================================================
; Description
;======================================================================================
Local $oDescription = _IEGetObjById($oIE, "pstad-descrptn")
_IEFormElementSetValue($oDescription, "Test-02 with minimum 10 chars")
;======================================================================================
; Code postal
;======================================================================================
Local $oCodePostal = _IEGetObjById($oIE, "addressPostalCode")
_IEFormElementSetValue($oCodePostal, "H1A1A1")
;======================================================================================
; Lieu
;======================================================================================
Local $oLieu = _IEGetObjById($oIE, "AddressCity")
_IEFormElementSetValue($oLieu, "Test-03")
;======================================================================================
; Ville
;======================================================================================
Local $oVille  = _IEGetObjById($oIE, "locationLevel0")
_IEFormElementOptionSelect($oVille, "1700281")
Local $oVilleHidden = _IEGetObjById($oIE, "pstad-loctnid")
_IEFormElementSetValue($oVilleHidden, "1700281")
;======================================================================================
; Téléphone
;======================================================================================
Local $oTelephone = _IEGetObjById($oIE, "PhoneNumber")
_IEFormElementSetValue($oTelephone, "555-555-5555")
;======================================================================================
; Adresse de courriel
;======================================================================================
Local $oEmail = _IEGetObjById($oIE, "pstad-email")
_IEFormElementSetValue($oEmail, "test@test1.com")
;======================================================================================

Exit

The script its smaller and faster. :)

Share this post


Link to post
Share on other sites

MichaelHB,

you found my solution with :

input type="hidden" id="pstad-loctnid" which I overlooked.

The script you wrote works fine as it is. 

I do not get the error :   error "Une erreur est survenue lors de l'affichage de votre annonce. Veuillez contact le Service à la clientèle." 

Thanks very much for your precious help.

GerryIT

1 person likes this

Share this post


Link to post
Share on other sites

Glad to help. :)

1 person likes this

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