hagaizenberg Posted January 14, 2011 Share Posted January 14, 2011 (edited) Hi Guys,Maybe you have an idea how to do the following...I'm using _IEFormElementOptionselect with $s_mode "byText",and it seems like the text/value that I give in the $s_string needs to be the exact text in the Html.but on the website I'm trying to automate, I have the text with spaces all around it, e.x.:<option value="20"> Australia </option>So I was wondering if I could somehow use $s_mode=byText but tell it not to search for an exact match, but to search for a StringInStr...So I could use the value "Australia" and it will search to see if "Australia" is within the string on the html option.And if it's not possible using the standard API, any idea how to do this??Thanks. Edited January 14, 2011 by hagaizenberg Link to comment Share on other sites More sharing options...
MrMitchell Posted January 14, 2011 Share Posted January 14, 2011 I wonder if you can you use s_Mode of "byValue"? It says your value is "20" right there in that line... Link to comment Share on other sites More sharing options...
hagaizenberg Posted January 14, 2011 Author Share Posted January 14, 2011 :-) well i can, but it's just an example, i want to use the text. i'm trying to write something generic that will select categories by their text, since the values are often numbers that are meaningless for my script. any other ideas? Link to comment Share on other sites More sharing options...
MrMitchell Posted January 14, 2011 Share Posted January 14, 2011 (edited) Yea in that case I think you're going to want to look into _IEFormElementGetCollection() Edit1: Once you have all the elements in an object collection you should be able to loop through them and check names or values to find what you want...I'm still trying to test it out. Edit2: Oops...well sorry I was thinking about something else, not sure how to do this one! But I'll stay tuned because I'd like to know when someone that does know posts how you can do this. Edited January 14, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
MrMitchell Posted January 14, 2011 Share Posted January 14, 2011 (edited) Have to leave now but this is what I've come up with so far... #include <IE.au3> _IEErrorHandlerRegister() $oIE = _IE_Example("form") $oForm = _IEFormGetCollection($oIE, 0) $textToLookFor = "freepage" _coolFunctionName($oForm, $textToLookFor) _IEErrorHandlerDeRegister() Exit Func _coolFunctionName($objForm, $findText) $oElements = _IEFormElementGetCollection($objForm) For $oElement In $oElements If $oElement.type = "select-one" Then $oOptions = $oElement.options For $oOption In $oOptions $optionText = String($oOption.text) If StringInStr($optionText, $findText) Then _IEFormElementOptionselect($oElement, $optionText, 1, "byText", 1) EndIf Next EndIf Next EndFunc So it basically goes thru the elements on the form and if it finds a select-one type of element, it will get all of its objects and go through the text in each. The first option it finds that contains $textToLookFor will be selected. You'll need to add error handling, and there's some redundancy and inefficient stuff that needs to be fixed too. But it should work just copy and paste to a new project and test it out. Edited January 14, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
PsaltyDS Posted January 15, 2011 Share Posted January 15, 2011 (edited) You only have to change four lines of code to modify Dale's original function to also use StringRegExp() pattern matching (added double underscore to name of function): expandcollapse popup#include <IE.au3> $oIE = _IE_Example("form") $oForm = _IEFormGetObjByName($oIE, "ExampleForm") $oSelect = _IEFormElementGetObjByName($oForm, "selectExample") For $i = 1 To 10 __IEFormElementOptionselect($oSelect, "r.+p", 1, "byRegExpText") ; Matches "reep" in "Freepage" Sleep(1000) __IEFormElementOptionselect($oSelect, "dip.+\.html", 1, "byRegExpValue") ; Matches "dipage.html" in "Midipage.html" Sleep(1000) Next ; #FUNCTION# ==================================================================================================================== ; Name...........: __IEFormElementOptionselect ; Description ...: Set the value of a specified form element ; Parameters ....: $o_object - Form Element Object of type "Select Option" ; $s_string - Value used to match element - treatment based on $s_mode ; $f_select - Optional: specifies whether element should be selected or deselected ; -1 = Return selected state ; 0 = Deselect the element ; 1 = (Default) Select the element ; $s_mode - Optional: specifies search mode ; byValue = (Default) value of the option you wish to select ; byRegExpValue = StringRegExp() pattern match to value ; byText = text of the option you wish to select ; byRegExpText = StringRegExp() pattern match to text ; byIndex = 0-based index of option you wish to select ; $f_fireEvent- Optional: specifies whether to fire an onchange event after changing value ; 0 = do not fire onchange event after setting value ; 1 = (Default) fire onchange event after setting value ; Return values .: On Success - If $f_select = -1, returns the current selected state, else returns 1 ; On Failure - Returns 0 and sets @ERROR ; @ERROR - 0 ($_IEStatus_Success) = No Error ; - 3 ($_IEStatus_InvalidDataType) = Invalid Data Type ; - 4 ($_IEStatus_InvalidObjectType) = Invalid Object Type ; - 5 ($_IEStatus_InvalidValue) = Invalid Value ; - 7 ($_IEStatus_NoMatch) = No Match ; @Extended - Contains invalid parameter number ; Author ........: Dale Hohm ; Modified ......: PsaltyDS - Added RegExp pattern matching ; =============================================================================================================================== Func __IEFormElementOptionselect(ByRef $o_object, $s_string, $f_select = 1, $s_mode = "byValue", $f_fireEvent = 1) If Not IsObj($o_object) Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidDataType") Return SetError($_IEStatus_InvalidDataType, 1, 0) EndIf ; If Not __IEIsObjType($o_object, "formselectelement") Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidObjectType") Return SetError($_IEStatus_InvalidObjectType, 1, 0) EndIf ; Local $oItems = $o_object.options, $iNumItems = $o_object.options.length, $f_isMultiple = $o_object.multiple Switch $s_mode Case "byValue", "byRegExpValue" For $oItem In $oItems If (($s_mode = "byValue") And ($oItem.value = $s_string)) Or (($s_mode = "byRegExpValue") And StringRegExp($oItem.value, $s_string, 0)) Then Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItem.selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItem.selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItem.selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch __IEErrorNotify("Warning", "_IEFormElementOptionselect", "$_IEStatus_NoMatch", "Value not matched") Return SetError($_IEStatus_NoMatch, 2, 0) EndIf Next Case "byText", "byRegExpText" For $oItem In $oItems If (($s_mode = "byText") And (String($oItem.text) = $s_string)) Or (($s_mode = "byRegExpText") And StringRegExp($oItem.text, $s_string, 0)) Then Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItem.selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItem.selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItem.selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch __IEErrorNotify("Warning", "_IEFormElementOptionselect", "$_IEStatus_NoMatch", "Text not matched") Return SetError($_IEStatus_NoMatch, 2, 0) EndIf Next Case "byIndex" Local $i_index = Number($s_string) If $i_index < 0 Or $i_index >= $iNumItems Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid index value, " & $i_index) Return SetError($_IEStatus_InvalidValue, 2, 0) EndIf $oItem = $oItems.item($i_index) Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItems.item($i_index).selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItems.item($i_index).selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItems.item($i_index).selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid Mode") Return SetError($_IEStatus_InvalidValue, 4, 0) EndSwitch EndFunc ;==>__IEFormElementOptionselect Edited January 15, 2011 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 More sharing options...
jayyx Posted February 26, 2014 Share Posted February 26, 2014 This solved a problem for me -- thanks @PsaltyDS. I'm not sure why the default _IEFormElementOptionSelect did not work even when I put in the exact text it was returning. ByValue and ByIndex worked fine but ByText just would not work. Here is an example of the text that would not work in the default function: scriptuser2 (scriptuser2) Link to comment Share on other sites More sharing options...
redline147 Posted August 21, 2014 Share Posted August 21, 2014 Hi I'm new in this forum but i'm working with Autoit for two years. I have de same problem i would like to select and option with _IEFormElementOptionselect() but this have blankspaces, and they don't work with whitespaces. And use the value is no good for my. <select name="newproduct_cat_parent" id="newproduct_cat_parent" class="postform" sourceindex="84"> <option value="-1">— Categoría de Producto superior —</option> <option class="level-0" value="217">CALEFACCION Y GAS</option> <option class="level-1" value="216"> CALDERAS</option> <option class="level-2" value="218"> SIME MURALES GAS</option> <option class="level-3" value="219"> SIME MURALES METROPOLIS</option> </select> I try to do the idea of PsaltyDS But i don't understand so much, If i put this script in a new blank script take error error: __IEErrorNotify(): undefined function. If I add the new modified function in <IE.au3> take the same error. What i need to do? Can anyone give me and idea?? How can i recive only the text of selected item <option class="level-0" value="217">CALEFACCION Y GAS</option> Sorry my english You only have to change four lines of code to modify Dale's original function to also use StringRegExp() pattern matching (added double underscore to name of function): expandcollapse popup#include <IE.au3> $oIE = _IE_Example("form") $oForm = _IEFormGetObjByName($oIE, "ExampleForm") $oSelect = _IEFormElementGetObjByName($oForm, "selectExample") For $i = 1 To 10 __IEFormElementOptionselect($oSelect, "r.+p", 1, "byRegExpText") ; Matches "reep" in "Freepage" Sleep(1000) __IEFormElementOptionselect($oSelect, "dip.+\.html", 1, "byRegExpValue") ; Matches "dipage.html" in "Midipage.html" Sleep(1000) Next ; #FUNCTION# ==================================================================================================================== ; Name...........: __IEFormElementOptionselect ; Description ...: Set the value of a specified form element ; Parameters ....: $o_object - Form Element Object of type "Select Option" ; $s_string - Value used to match element - treatment based on $s_mode ; $f_select - Optional: specifies whether element should be selected or deselected ; -1 = Return selected state ; 0 = Deselect the element ; 1 = (Default) Select the element ; $s_mode - Optional: specifies search mode ; byValue = (Default) value of the option you wish to select ; byRegExpValue = StringRegExp() pattern match to value ; byText = text of the option you wish to select ; byRegExpText = StringRegExp() pattern match to text ; byIndex = 0-based index of option you wish to select ; $f_fireEvent- Optional: specifies whether to fire an onchange event after changing value ; 0 = do not fire onchange event after setting value ; 1 = (Default) fire onchange event after setting value ; Return values .: On Success - If $f_select = -1, returns the current selected state, else returns 1 ; On Failure - Returns 0 and sets @ERROR ; @ERROR - 0 ($_IEStatus_Success) = No Error ; - 3 ($_IEStatus_InvalidDataType) = Invalid Data Type ; - 4 ($_IEStatus_InvalidObjectType) = Invalid Object Type ; - 5 ($_IEStatus_InvalidValue) = Invalid Value ; - 7 ($_IEStatus_NoMatch) = No Match ; @Extended - Contains invalid parameter number ; Author ........: Dale Hohm ; Modified ......: PsaltyDS - Added RegExp pattern matching ; =============================================================================================================================== Func __IEFormElementOptionselect(ByRef $o_object, $s_string, $f_select = 1, $s_mode = "byValue", $f_fireEvent = 1) If Not IsObj($o_object) Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidDataType") Return SetError($_IEStatus_InvalidDataType, 1, 0) EndIf ; If Not __IEIsObjType($o_object, "formselectelement") Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidObjectType") Return SetError($_IEStatus_InvalidObjectType, 1, 0) EndIf ; Local $oItems = $o_object.options, $iNumItems = $o_object.options.length, $f_isMultiple = $o_object.multiple Switch $s_mode Case "byValue", "byRegExpValue" For $oItem In $oItems If (($s_mode = "byValue") And ($oItem.value = $s_string)) Or (($s_mode = "byRegExpValue") And StringRegExp($oItem.value, $s_string, 0)) Then Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItem.selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItem.selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItem.selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch __IEErrorNotify("Warning", "_IEFormElementOptionselect", "$_IEStatus_NoMatch", "Value not matched") Return SetError($_IEStatus_NoMatch, 2, 0) EndIf Next Case "byText", "byRegExpText" For $oItem In $oItems If (($s_mode = "byText") And (String($oItem.text) = $s_string)) Or (($s_mode = "byRegExpText") And StringRegExp($oItem.text, $s_string, 0)) Then Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItem.selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItem.selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItem.selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch __IEErrorNotify("Warning", "_IEFormElementOptionselect", "$_IEStatus_NoMatch", "Text not matched") Return SetError($_IEStatus_NoMatch, 2, 0) EndIf Next Case "byIndex" Local $i_index = Number($s_string) If $i_index < 0 Or $i_index >= $iNumItems Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid index value, " & $i_index) Return SetError($_IEStatus_InvalidValue, 2, 0) EndIf $oItem = $oItems.item($i_index) Switch $f_select Case -1 Return SetError($_IEStatus_Success, 0, $oItems.item($i_index).selected) Case 0 If Not $f_isMultiple Then __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", _ "$f_select=0 only valid for type=select multiple") SetError($_IEStatus_InvalidValue, 3) EndIf If $oItem.selected Then $oItems.item($i_index).selected = False If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case 1 If Not $oItem.selected Then $oItems.item($i_index).selected = True If $f_fireEvent Then $o_object.fireEvent("onchange") $o_object.fireEvent("onclick") EndIf EndIf Return SetError($_IEStatus_Success, 0, 1) Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid $f_select value") Return SetError($_IEStatus_InvalidValue, 3, 0) EndSwitch Case Else __IEErrorNotify("Error", "_IEFormElementOptionselect", "$_IEStatus_InvalidValue", "Invalid Mode") Return SetError($_IEStatus_InvalidValue, 4, 0) EndSwitch EndFunc ;==>__IEFormElementOptionselect Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now