select drop-down list by text not value - excel

I am trying to write an Excel VBA script to navigate to a website and select from a drop-down list using the text in the list item, NOT the value of the index. By examining the HTML code, I can see that the values in the drop-down list appear to be integers with no recognizable pattern and do not appear to correlate to the text shown for the entry in the drop-down list.
<option value="246">new2</option>
<option value="245">new</option>
<option value="196">test</option>
I can successfully use the following command:
ieDoc.getElementById("viewMaintainFixedCombustible_fixedCombustible_fireZoneId").Value = 246
To select the item "new2" from the drop-down list, by sending the value 246. Or, I could send 245 to select "new". However, the elements in the list are always changing, so selecting by value (246, 245, etc.) is impractical. Thus, I am searching for a way to select an entry from the drop-down list (e.g. "new2") by sending the text name of the entry (e.g. "new2") in VBA.
Does anyone have a method for how to do this?

Try below sample code to set dropdown list by text.
Set drp =ieDoc.getElementById("<id of your dropdown>")
For x = 0 To drp.Options.Length - 1
If drp.Options(x).Text = "new2" Then
drp.selectedIndex = x
Exit For
End If
Next

Related

How to get texts from screen displaying an "ABAP List"

I am able to enter to the operation in SAP GUI via VBA but I am unable to find how to select or copy the value of these fields.
While recording the script via SAP GUI, copying the fields into the clipboard won't appear in the script as an action.
Any help or reference is highly appreciated.
Please find code and screenshot below (screen of type "ABAP List").
Sub XXXX()
Call SAPConnections
Session.FindById("wnd[0]/tbar[0]/okcd").Text = "Operation"
Session.FindById("wnd[0]").SendVKey 0
Session.FindById("wnd[0]/usr/ctxtP_MATRL").Text = "PartNumber"
Session.FindById("wnd[0]").SendVKey 8
Session.FindById("wnd[0]/tbar[1]/btn[1]").Press
The screen you are showing is named an "ABAP List".
It contains labels and text fields, they belong to the property Children of the GuiUserArea object, and their IDs are made of prefix lbl or txt followed by the column and row numbers. The respective SAP GUI Scripting objects are GuiLabel and GuiTextField, for example:
label at column 0 (first column) row 12 (object GuiLabel):
/app/con[0]/ses[0]/wnd[0]/usr/lbl[0,12]
text field at column 22 row 12 (object GuiTextField):
/app/con[0]/ses[0]/wnd[0]/usr/txt[22,12]
checkbox at column 0 row 0 (first row) (object GuiCheckBox):
/app/con[0]/ses[0]/wnd[0]/usr/chk[0,0]`
If you want to know all the fields which are in the ABAP list, you must loop at the property Children of the GuiUserArea object.
EDIT: checkbox added in the list above. I think we now have all the possible types of fields for an ABAP List (I guess other simple graphical elements objects like GuiButton, GuiComboBox, GuiCTextField, GuiRadioButton, are not possible).
EDIT: see this more detailed answer with a script to loop at all ABAP List fields and example with a screen and corresponding property values.

Issue iterating over items with Robot Framework

I am writing a Keyword using Robot Framework with FOR Loop just to iterate all the values present in the 1ST dropdown and get selected one by one. Loop is working but not able to iterate for selecting the indexes.
WebSite:
https://blazedemo.com/ (Choose your departure city:)
Test Case is:
1st will select the zero index with value "Paris", then will select the First index with value "Philadelphia" so on till the 6th indexes.
XPATH:
${Xpath_Departure_DropDown}= "xpath://*[starts-with(#class,'container')]//select[#name='fromPort']"
${Xpath_Departure_DropDown_Options}= "xpath://select[#name='fromPort']/option"
Robot Framework Keyword:
KW_14: Choose your departure city Drop Down:
#{All_Items_In_Drop_Down}= Get List Items ${Xpath_Departure_DropDown} values=True
${All_Items_In_Drop_Down_Label}= Get Selected List Label ${Xpath_Departure_DropDown}
${All_Items_In_Drop_Down_Value}= Get Selected List Value ${Xpath_Departure_DropDown}
List Selection Should Be ${Xpath_Departure_DropDown} ${All_Items_In_Drop_Down_Value}
${Length_Of_Lists_Items}= get length ${All_Items_In_Drop_Down}
FOR ${Index} IN RANGE ${Length_Of_Lists_Items}
LOG ${Index} html=true
Click Element ${Xpath_Departure_DropDown}
sleep 2 seconds
run keyword and continue on failure Select From List By Index ${Xpath_Departure_DropDown_Options}[${Index}]
sleep 2 seconds
${All_Items_In_Drop_Down_Label}= Get Selected List Label ${Xpath_Departure_DropDown}
${All_Items_In_Drop_Down_Value}= Get Selected List Value ${Xpath_Departure_DropDown}
List Selection Should Be ${Xpath_Departure_DropDown} ${All_Items_In_Drop_Down_Value}
END
I believe your usage of "Select From List By Index" is incorrect. Please check docs:
https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Select%20From%20List%20By%20Index
It accepts locator and index. While you are passing
${Xpath_Departure_DropDown_Options}[${Index}]
which doesn't make sense at all since Xpath_Departure_DropDown_Options is locator itself. Try
Select From List By Index ${Xpath_Departure_DropDown_Options} ${Index}

Excel VBA: Making userform with dynamic comboboxes, content of which changes depending on data of parental combobox

The idea is to build a userform that have numerous "lines" of two comboboxes to choose from: one let choose a category (main filed) and the second one (dependent filed) let you choose subcategory for the category user have chosen in main field.
Number of this comboboxes pairs may be variable and chosen by user - for adding or deleting line of two comboboxes there are buttons on the top. When pressing "Add Field" button, userform adds new pair at the bottom and gives each combobox name with ordinal number at the end, e.g.:
Main_field_1, Dependent_field_2;
Main_field_2, Dependent_Field_2
and so on.
Visual example of userform
But there is a problem: if no name of combobox is predetermined, how can a code be written for them?
For example I want to register a change in Main_field to be able to run a code, that will fill in data for Dependent_field, based on Main_field value. But if this comboboxes were created by user with "Add field" button there was no code written for them like:
Private Sub Main_field_1_Change()
Select Case UserForm1.Main_field_1.Value
Case "Category1"
UserForm1.Dependent_field_1.AddItem "SubCategory_1"
UserForm1.Dependent_field_1.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_1.AddItem "SubCategory_88"
UserForm1.Dependent_field_1.AddItem "SubCategory_99"
Case Else
End Select
End Sub
because name of main and dependent fields have variables in them, like Main_field_999 and Dependent_field_999 (the numbers at the end will be identical for each line) and as i understand there is code can exist like this:
Private Sub Main_field_XX_Change()
Select Case UserForm1.Main_field_XX.Value
Case "Category1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_XX.AddItem "SubCategory_88"
UserForm1.Dependent_field_XX.AddItem "SubCategory_99"
Case Else
End Select
End Sub
The question is: how can a code be written to recognize a change in combobox, that have a variable name to then execute different piece of code, based on value in this combobox?

Excel-VBA combo box value on form load

I have a VBA form which is used to enter data on a sheet. I am currently coding the form so as it will load any data already existing in the sheet back into the form.
For simple text strings it works perfectly.
e.g.
ReqSetup.ReqText = Application.Worksheets("Req Sheet").Range("F11").Value
However, I have some combo boxes, that on the form, when they are selected will enter a number in the corresponding cell.
Fail 1. - Run Time Error 380 - Invalid property value.
ReqSetup.MinPerKgCB = Application.Worksheets("Req Sheet").Range("C27").Value
Fail 2.
Dim MinPerKg As Range
Set MinPerKg = Application.Worksheets("Req Sheet").Range("C27")
ReqSetup.MinPerKgCB = MinPerKg
I'm obviously doing something really simple wrong but I can't work out what it is!!
Kind Regards!
I have some combo boxes, that on the form, when they are selected will
enter a number in the corresponding cell
Then you'd need to do the opposite of your code attempt, i.e.:
Worksheets("Req Sheet").Range("C27").Value = ReqSetup.MinPerKgCB.Value
That you'd better wrap inside a check that any combobox value is actually selected :
With ReqSetup.MinPerKgCB
If .ListIndex <> -1 Then Worksheets("Req Sheet").Range("C27").Value = .Value
End With

Keyboard shortcuts in an AppleScript list dialog

I am looking for a way to assign keyboard shortcuts to the items in a list dialog in AppleScript.
I am using the following to display a long list of folder names and am looking for an easy way to select an item from the list.
set selectedFolderName to {choose from list folderList}
Currently the list displays like :
Office
Personal
Projects
...
Vendors
and I have to navigate down the list with the cursor keys or mouse to select an item. I would like to be able to show either :
a) Office
b) Personal
c) Projects
...
m) Vendors
or :
Office
Personal
pRojects
...
Vendors
Then I could press the C key (first example) or R key (second example) to select 'Projects'.
I studied the AppleScript documentation, such as it is, and searched far and wide, but have not been able to spot a way to accomplish this.
I'm not totally sure if this is what you want, but I use the construct below, so that I can press "a", to select the first item, "b" to select the second item, and so on.
set litems to {"apples", "pears", "banana", "oranges", "plums", "grapes"}
set deli to "." & space & space
repeat with i from 1 to (count litems)
set item i of litems to character id (i + 96) & deli & item i of litems
end repeat
log litems
set theRes to choose from list litems
(*a. apples, b. pears, c. banana, d. oranges, e. plums, f. grapes*)
set origItem to text 5 thru -1 of item 1 of theRes
log origItem
—> plums
The second version of what you're asking is not possible. But, the first is. There is nothing built in for this kind of functionality, but you can build your own sub-routine for it. You can prepend your list items with letters or numbers (I'll use numbers since it's simpler).
on choose_from_list_with_shortcut(ls)
-- prepend the choices with "1) "
repeat with i from 1 to (count ls)
set item i of ls to (i as string) & ") " & item i of ls
end repeat
set chosenItems to choose from list ls
-- strip the prefixes from the chosen items
repeat with i from 1 to (count chosenItems)
set item i of chosenItems to text 4 thru -1 of item i of chosenItems
end repeat
return chosenItems
end choose_from_list_with_shortcut
Another option is to just start typing. Just like in the Finder, if you type the letters "pro," the list will highlight "Projects."

Resources