2

With WinAppDriver and VB.NET, I am trying to get an item selected in a Combobox. I need to select it directly, not by keyboard typing or cursor key navigation as that triggers other program actions on the not-to-be-selected items.

I've been trying to select the item with .FindElementByXPath and .click.

I used WinAppRecorder to get the xpath, which I removed unnecessary repeating identifiers from. The following xpath and code works to get the combobox to drop down:

Dim xStateCbo As String = "/Pane[@Name=""Desktop 1""][@ClassName=""#32769""]/Window[@AutomationId=""WorkItemForm""]/Window[@AutomationId=""ADlg""]/Pane[@AutomationId=""flpMain""]/Pane[@AutomationId=""pnlTopSection""]/Pane[@AutomationId=""pnlState""]/ComboBox[@AutomationId=""cboState""]"
ProgramSession.FindElementByXPath(xStateCbo).Click()
Threading.Thread.Sleep(1000) 'make sure cbo has time to open

After getting the cbo to drop down, I then try to select the desired item by clicking on it.

Recording a click with the WinAppRecorder, I get this code (converted to VB.NET):

Dim xp4 As String = "/Pane[@Name=""Desktop 1""][@ClassName=""#32769""]/List[@Name=""State of Residence: ""][@ClassName=""ComboLBox""]/ListItem[@Name=""DELAWARE""]"
ProgramSession.FindElementByXPath(xp4).Click()

(Note that the xpath for clicking the item in the open cbo is shorter than for clicking the cbo in the first place, likely because of the way that some cbos open in a higher level. I think this is normal and not related to the problem.)

The above code should now find and click "DELAWARE" in my now-open combobox, but instead I get the standard exception for not finding an element:

System.InvalidOperationException: 'An element could not be located on the page using the given search parameters.'

It's not a casing issue, everything in the cbo is all in caps. It's not a timing issue, as I've made sure the cbo is fully dropped-down and populated.

I've also tried searches with clicks also with .FindElementByName, etc. and identifiers from Inspect.exe, but no luck with those either. I don't care what solution is used to select the target items. I'm open to whatever works reliably.

ORNS
  • 107
  • 8
  • Since XPath expressions work with the xml found in your PageSource property from your driver, did you check if the PageSource contains that specific combobox item? Also, check if you are able to find those string values from your second XPath expression in that xml. – PixelPlex Apr 26 '19 at 12:00
  • I looked at ```ProgramSession.PageSource``` both before and after the click to make the cbo drop down. No mention of the cbo inside it – ORNS Apr 26 '19 at 13:27
  • Its a bit unlikely, but are you sure you have the correct window handle? Every window handle has it's own page source so maybe you are looking at the xml from the wrong window? If you have several window handles with similar page sources, the possibility exists that you still find elements (your first XPath expression) but not all elements (your second XPath expression), on the wrong page source. You can check this by comparing the ID from your current window handle with the ones from the available window handles. – PixelPlex Apr 26 '19 at 14:00
  • I have 3 handles (0 thru 2) from ```.WindowHandles.``` The program has 2 "main windows" and one dialog open at the point above in code. Handles 0 and 2 go to the "main windows". Handle 1 seems to go to a "phantom" window that is never seen on screen, and is present before the screen (Handle 0) that spawns the dialog that contains the cbo. I've tried ```.SwitchTo().Window(0)``` with various combinations of the xpath above, and no luck yet. Still get same object not found ex – ORNS Apr 26 '19 at 18:57
  • I've read some other SO post saying window handles get added chronological, so the last index (2) should always be the most recently added index. Having handle index 1 present before index 0 does not seem to make sense. If the combobox is a custom control or a 3th party control, this could give you problems to (reduced accessibility). As a last debugging resort, you could try to isolate the problem in a small project containing a window and a combobox with a few items on it. If you still get that problem, the list with possible reasons why its broken should be considerably fewer. – PixelPlex Apr 29 '19 at 06:58

1 Answers1

0

I'm not quite sure why this works, but it seems to mirror how Combobox items are selected manually in Windows using just the keyboard.

Locate the Combobox and then use .SendKeys to send the ComboBox the unique first letter of the required Combolist item. In C#:-

''' driver.FindElementByAccessibilityId("CombolistId").SendKeys("P"); '''

Selects the item from the Combobox list beginning with the "P" character. I have not tried the situation in which 2 Combolist items start with the same first letter.

Tony H
  • 61
  • 3