NoSuchElement when tapping in Chrome Android

Hi! I have this very simple code set up to click on an element on a web page (in C#):

var element = driver.FindElement(By.XPath("//p"));
touchAciton = new TouchAction((IPerformsTouchActions)driver);
touchAciton.Tap(element).Perform();

It finds the element, because I can get the text value from it. However, the tap action will always fail with NoSuchElementException. Here’s the log close to the error:

[HTTP] --> POST /wd/hub/session/f0c90577-2978-46e4-9cb4-997a909ba492/element
[HTTP] ******{"using":"xpath","value":"//p"}******
[W3C (f0c90577)] Driver proxy active, passing request on via HTTP proxy
[debug] [WD Proxy] Matched '/wd/hub/session/f0c90577-2978-46e4-9cb4-997a909ba492/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /wd/hub/session/f0c90577-2978-46e4-9cb4-997a909ba492/element] to [POST http://127.0.0.1:8001/wd/hub/session/7df58ee885c52d435ad1426e26fd44b3/element] with body: {"using":"xpath","value":"//p"}
[debug] [WD Proxy] Got response with status 200: {"sessionId":"7df58ee885c52d435ad1426e26fd44b3","status":0,"value":{"ELEMENT":"0.7968653201754161-2"}}
[WD Proxy] Replacing sessionId 7df58ee885c52d435ad1426e26fd44b3 with f0c90577-2978-46e4-9cb4-997a909ba492
[HTTP] <-- POST /wd/hub/session/f0c90577-2978-46e4-9cb4-997a909ba492/element 200 38 ms - 156
[HTTP]
[HTTP] --> POST /wd/hub/session/f0c90577-2978-46e4-9cb4-997a909ba492/touch/perform
[HTTP] ******{"actions":[{"action":"tap","options":{"element":"0.7968653201754161-2"}}]}******
[debug] [W3C (f0c90577)] Calling AppiumDriver.performTouch() with args: [[{"action":"tap","options":{"element":"0.7968653201754161-2"}}],"f0c90577-2978-46e4-9cb4-997a909ba492"]
[UiAutomator2] calling get location: 0.7968653201754161-2
[debug] [WD Proxy] Matched '/element/0.7968653201754161-2/location' to command name 'getLocation'
[debug] [WD Proxy] Proxying [GET /element/0.7968653201754161-2/location] to [GET http://127.0.0.1:8200/wd/hub/session/776ac0aa-382a-4420-8441-dc88c967cfb8/element/0.7968653201754161-2/location] with no body
[WD Proxy] Got response with status 404: {"sessionId":"776ac0aa-382a-4420-8441-dc88c967cfb8","value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters"

Where is the second element name coming from and why can’t it find it, but only when tapping?

Touch actions only work in native context and thus they cannot be executed on web elements. It is only possible to execute them with absolute coordinates being in web view context.

1 Like

Thanks, that makes sense!
So I just switched context and clicked on the element’s bounding box center, relative to the webview coordinates, then switched back to the webview to continue with the test (for posterity):

// switch to native context
var driver = ((AndroidDriver<AppiumWebElement>)Driver);
string originalContext = driver.Context;
driver.Context = "NATIVE_APP";

var webView = driver
    .FindElement(By.XPath("//android.webkit.WebView"));

var touchAciton = new TouchAction((IPerformsTouchActions)Driver);
touchAciton.Tap(webView, x, y).Perform();

// switch back to original context
driver.Context = originalContext;