-1

WinAppDriver's FindElement will not always find objects in the program to be automated.

I've gotten this to work with other programs, like Notepad, and even a different dialog in my program to be automated, and it worked in those places.

This is the code I am using so far. The first three lines execute without error, successfully launching the application into it's Login dialog:

Dim appCapabilities As DesiredCapabilities = New DesiredCapabilities()
appCapabilities.SetCapability("app", "C:\[my program].exe")
Dim ProgramSession = New WindowsDriver(Of WindowsElement)(New Uri("http://127.0.0.1:4723"), appCapabilities)

ProgramSession.FindElementByName("Password").SendKeys("Password")

The fourth line should find the element, a text box, and enter the string "Password" into it via sendkeys, but it fails, with the following exception:

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

The target object is on screen, and this should work. I'm using the info shown for the object in Inspect.exe, Name: "Password".

WinAppDriver's window shows the following error information:

{"using":"name","value":"Password"}
HTTP/1.1 404 Not Found
Content-Length: 139
Content-Type: application/json

{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}

Community
  • 1
  • 1
ORNS
  • 107
  • 8

2 Answers2

0

The fourth line of code is executed directly after program startup. Since a program needs some load time, you'll need to wait for the program to finish loading before trying to search for a control on the GUI. You can do this by using a while loop in combination with a stopwatch for timeout.

Dim shouldContinue As Boolean = True
Dim stopWatch As StopWatch = New StopWatch()
Dim timeOut As TimeSpan = TimeSpan.FromSeconds(30)
stopWatch.Start()

While shouldContinue AndAlso timeOut > stopWatch.Elapsed

    If element.IsFound Then
        shouldContinue = False
        stopWatch.Stop()
    End If
End While

element.IsFound is just mock-up code, you will need to fill in that blank. This is a good Q/A to show you how to check if a element has loaded.

Another thing you need to take in account is the possibility that your Login Dialog runs in another window handle. If the window handle winappdriver is using is different from the window handle where your element is at, you won't be able to find that element.

Also check if you can find whatever you are searching for in the PageSource property xml from your driver. I usually do this by calling that property in the visual studio watch window, and copying it's content to a xml formatter tool.

PixelPlex
  • 749
  • 5
  • 21
  • I was using a breakpoint to make sure that the login dialog was present for line 4. I should have mentioned that though... – ORNS Apr 24 '19 at 12:56
  • lol, yup, that would have been helpful. The effort was not a complete waste, I probably provided you some useful code for handling the program loading :-). – PixelPlex Apr 24 '19 at 13:03
  • I was able to get the objects to resolve via xpath, so I'm not sure if the handles were a part of the problem using FindElementByName or not. – ORNS Apr 24 '19 at 13:03
  • Ah ok. The handles won't be your problem than. The login page probably didn't have a element that goes by the name of "Password", you could check with the devs who made the program to be sure. One small tip, if you have problems finding elements, it's a good idea to post your `PageSource` xml along with the code that queries the gui. – PixelPlex Apr 24 '19 at 13:08
0

I was able to find the password field by using FindElementByXPath instead of FindElementByName.

In order to find the xpath, I used the Recorder for WinAppDriver.

These xpaths can be VERY long. I was able to shorten some of them by removing some duplicate attributes, but some are over 450 characters long. I can sometimes reduce it further with variables, but I'm not exactly delighted so far with WinAppDriver as a replacement for CodedUI.

ORNS
  • 107
  • 8
  • Yeah, I'm also coming from CodedUI. I needed a little time to get used to using XPath, but once I did, I found it's a very powerful way of querying a gui. I found [this](https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256086%28v%3dvs.100%29) resource very useful. – PixelPlex Apr 24 '19 at 13:25