Appium for a windows application: Cannot find element after double-click

2.3k Views Asked by At

Using Appium 5.0.0-beta to automate a test for a desktop application on Windows.

I can open the application and find elements with no problem, but after double-clicking a button which modifies a part of the window, Appium can no longer locate any elements. If I look at the PageSource, I can see that the elements ARE there.

1.) Initialising and opening the app:

string path = @"{ApplicationFilePathExe}";
AppiumOptions options = new AppiumOptions();
options.App = path;
_session = new WindowsDriver(new Uri("http://localhost:4723/wd/hub"), options);

// Make sure focus is on app window
var handles = _session.WindowHandles;
_session.SwitchTo().Window(handles[0]);
Assert.AreEqual(_session.CurrentWindowHandle, handles[0]);

2.) Double-clicking an element (found by XPath via automation ID), which opens a new UI segment:

var button = GetElement({ButtonAutomationID});
DoubleClick(button);

// GetElement method
public AppiumElement GetElement(string automationId)
{
    var element = _session.FindElement(By.XPath($"//*contains(@AutomationId,'{automationId}')]"));
    if (element == null)
    {
        throw new NoSuchElementException($"Element with AutomationId='{automationId}' was not found.");
    }
    return element;
}

3.) Finding an element in the new UI segment:

var pageSource = _session.PageSource; // THIS CONTAINS THE ELEMENT I'M SEARCHING FOR
var importElement = GetElement("{NewElementName}"); //THROWS ERROR

This last step gives an error:

"An element could not be located on the page using the given search parameters."

This error is thrown regardless of which element I try to find, including elements which have already been found in previous steps and were not added/removed/modified by the button press.

Error stack trace:

   at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Appium.AppiumDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.WebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.By.<.ctor>b__11_0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.WebDriver.FindElement(By by)
   at OpenQA.Selenium.Appium.AppiumDriver.FindElement(By by)

Appium log:

[HTTP] --> POST /wd/hub/session/e8f89068-83b0-4f1c-8bf9-deb38801d6e1/element
[HTTP] {"using":"xpath","value":"//*[contains(@AutomationId,'{automationId}')]"}
[debug] [W3C (e8f89068)] Calling AppiumDriver.findElement() with args: ["xpath","//*[contains(@AutomationId,'{automationId}')]","e8f89068-83b0-4f1c-8bf9-deb38801d6e1"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, accessibility id
[debug] [WD Proxy] Matched '/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:4724/wd/hub/session/8B988823-95B3-4BAE-B0DA-54B3DD557868/element] with body: {"using":"xpath","value":"//*[contains(@AutomationId,'Framework3Tasks:Import/Export')]"}
[debug] [WinAppDriver] ==========================================
[debug] [WinAppDriver] POST /wd/hub/session/8B988823-95B3-4BAE-B0DA-54B3DD557868/element HTTP/1.1
[debug] [WinAppDriver] Accept: application/json, */*
[debug] [WinAppDriver] Connection: keep-alive
[debug] [WinAppDriver] Content-Length: 88
[debug] [WinAppDriver] Content-Type: application/json; charset=utf-8
[debug] [WinAppDriver] Host: 127.0.0.1:4724
[debug] [WinAppDriver] User-Agent: appium
[WD Proxy] Got response with status 404: {"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
[debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError
[debug] [WinAppDriver] HTTP/1.1 404 Not Found
[debug] [WinAppDriver] Content-Length: 139
[debug] [WinAppDriver] Content-Type: application/json
[debug] [WinAppDriver]
[debug] [WinAppDriver] {"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
[debug] [W3C (e8f89068)] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.
[debug] [W3C (e8f89068)]     at errorFromW3CJsonCode (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:780:25)
[debug] [W3C (e8f89068)]     at ProxyRequestError.getActualError (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:663:14)
[debug] [W3C (e8f89068)]     at WADProxy.command (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:272:19)
[debug] [W3C (e8f89068)]     at runMicrotasks (<anonymous>)
[debug] [W3C (e8f89068)]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[debug] [W3C (e8f89068)]     at WinAppDriver.sendCommand (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-windows-driver\lib\winappdriver.js:224:12)
[debug] [W3C (e8f89068)]     at WindowsDriver.findElOrEls (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-windows-driver\lib\commands\find.js:11:10)
[debug] [W3C (e8f89068)]     at WindowsDriver.findElOrElsWithProcessing (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\commands\find.js:33:12)
[debug] [W3C (e8f89068)]     at WindowsDriver.findElement (C:\Users\username\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\commands\find.js:53:10)
[HTTP] <-- POST /wd/hub/session/e8f89068-83b0-4f1c-8bf9-deb38801d6e1/element 404 1470 ms - 1562
1

There are 1 best solutions below

2
On

I think most probably the element may be "there", but it may be on a different frame. Try changing the frame to see if it works.

The website may be also fighting your automation, dynamically changing the id's or XPath that you think you are looking for.

Try finding the element by something else than the id or XPath, for example:

  • Name
  • Class Name
  • Tag Name
  • Link Text
  • Partial Link Text
  • CSS Selector

Also, check if the element is visible and enabled before interacting with it. Some buttons are perfectly visible all the time, but switch between enabled and disabled all the time.

  • WebElement.isDisplayed()
  • WebElement.isEnabled()