Jump to content

Help selecting item in dropdown box


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

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 SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

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 * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

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 * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

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 *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

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

Signature last update: 2023-04-24

Link to comment
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...

Link to comment
Share on other sites

_IEFormElementOptionSelect($oIE_Select, '1700281', 1, "byValue", 0)

?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

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 SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

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 * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

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 * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

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 *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

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

Signature last update: 2023-04-24

Link to comment
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

Link to comment
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

Link to comment
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 "--------".

Link to comment
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
;======================================================================================

 

Link to comment
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. :)

Link to comment
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

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