简体   繁体   中英

winappdriver couldn't find element using

I am new to automation and I am trying to automate the WPF application using WinAppDriver with C#. I am able to load the application but getting the error like {"An element could not be located on the page using the given search parameters."} while trying to find the element with Name/AccessibilityId even after keeping the wait time.

See below:

POST /session/09551C9F-CF20-4C2B-A900-F17D2483F9D8/element HTTP/1.1
Accept: application/json, image/png
Content-Length: 45
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"accessibility id","value":"TxtPwd"}
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."}}

I don't know what is happening. Any suggestions?

I did like - check for the elements and automation-id/name of element through inspect tool - set developer mode active - wait time before finding the element

 var aDesiredCapabilities = new DesiredCapabilities();
             aDesiredCapabilities.SetCapability("app", @"PathToApplication");
             aDesiredCapabilities.SetCapability("deviceName", "Windows 10");

             var aWindow = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), aDesiredCapabilities);
             aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Clear").Click();

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Login");

Is this user name password field showing in a pop-up?

Once you launch the application, put a short sleep before trying to access application UI elements. I suggest the following.

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));

A better way would be to use an instance of WebDriverWait class to wait until the element is loaded.

WebDriverWait wdv = new WebDriverWait(sessionAppWinForms, TimeSpan.FromSeconds(10));
var txtPwd = aWindow.FindElementByAccessibilityId("TxtPwd");
wdv.Until(x => txtPwd.Displayed);

Update: I suggest inspecting UI controls with WinAppDriver UI Recorder. The latest version didn't work on my PC that's why I recommend using version 1.0. The download link is given below. https://github.com/microsoft/WinAppDriver/releases/tag/UiR_v1.0-RC

WinAppDriver is just a helper program, you can create automation scripts without using it. Sometimes the application takes a little longer to launch, in such cases you might use the WebDriverWait class to wait for certain conditions to be true. For example, wait for a certain label or textbox to be present on the screen. You might use the following line of code to wait unconditionally for a few seconds.

System.Threading.Thread.Sleep(5000);

I teach a Udemy course about test automaton with WinAppDriver in C#.Net. These concepts are covered in detail. You may see it here .

The application may be opened in another window out of your aWindow scope.

You can try to create a desktop driver session and start your process using Process.Start() method.

If your application is running as administrator, then both WinAppDriver & Inspect.exe must run as administrator as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM