While automating a mobile test with Appium in C#, I can successfully click the first element 'Nieuw' using XPath.
However, when I try to locate and click another element with the accessibility id 'Proefrit', Appium throws a NoSuchElementException.
Here’s the relevant code:
IWebElement nieuw = (IWebElement)driver.FindElementByXPath("(//android.view.View[@content-desc='Nieuw'])[2]");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(nieuw));
nieuw.Click();
IWebElement proefrit = (IWebElement)driver.FindElementByAccessibilityId("Proefrit");
wait.Until(ExpectedConditions.ElementToBeClickable(proefrit));
proefrit.Click();
Appium Desktop can locate the element successfully, but the test code fails to find it.
I suspect the element might not be in focus or visible when the test tries to interact with it, possibly due to being inside another frame or view.
How can I ensure the element is properly located and interacted with when using FindElementByAccessibilityId?
@mehta_tvara I’ve run into this issue before.
Even if Appium Desktop can see the element, it doesn’t always mean the driver can interact with it immediately.
Sometimes the element isn’t fully rendered or is off-screen.
One trick that worked for me is scrolling or waiting until the element is visible before trying to click it:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement proefrit = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.AccessibilityId("Proefrit")));
proefrit.Click();
This ensures the driver waits for the element to actually appear in the view hierarchy.
From my experience, another common cause is that the element might be inside a different view hierarchy or frame after the first click.
Try adding a small wait after clicking ‘Nieuw’ to let the new view load, or use FindElements and pick the correct instance if there are multiple:
var proefritElements = driver.FindElementsByAccessibilityId("Proefrit");
if (proefritElements.Count > 0)
{
proefritElements[0].Click();
}
Sometimes FindElementByAccessibilityId fails if the element isn’t fully present yet, while FindElements gives a safer way to check.
I ran into a similar problem and found that some mobile elements are interactable only after scrolling into view.
You can use TouchAction to bring it into focus:
var proefrit = driver.FindElementByAccessibilityId("Proefrit");
new TouchAction(driver)
.Press(proefrit)
.Release()
.Perform();
Or, if it’s part of a RecyclerView or scrollable container, scroll to it first.
Appium can see it in the inspector, but if it’s off-screen, a direct click will fail.