Find element by Id gives error An element could not be located on the page using the given search parameters

appium 2.1.3(installed from npm)
appium desktop version 1.20.4
windows 10
android 3.1 2022
Here is my code:
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Android;

namespace DeviceTesting
{
[TestClass]
public class MobileTest
{
AppiumDriver driver;

    [TestMethod]
    public void SetCapabilities()
    {

        AppiumOptions cap = new AppiumOptions();
        cap.AddAdditionalCapability("deviceName", "HUAWEI Y6 Prime 2019");
        cap.AddAdditionalCapability("udid", "5HRNW19802009817");
        cap.AddAdditionalCapability("platformName", "Android");
        cap.AddAdditionalCapability("platformVersion", "9");
        cap.AddAdditionalCapability("appPackage", "com.aw.smartcompanion.development");
        cap.AddAdditionalCapability("appActivity", "com.aw.smartcompanion.development.MainActivity");
        cap.AddAdditionalCapability("adbExecTimeout", "50000");
        cap.AddAdditionalCapability("newCommandTimeout", "10000");
        cap.AddAdditionalCapability("appWaitForLaunch", "false");

        driver = new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap);
        Thread.Sleep(30000);

        AppiumWebElement btn = driver.FindElementById("com.android.packageinstaller:id/permission_allow_button");
        btn.Click();

        AppiumWebElement name = driver.FindElementById("usernameBox");
        name.Click();
        name.SendKeys("MS HUAWEI");

        AppiumWebElement locator = driver.FindElement(By.XPath("//android.widget.EditText[2]"));
        locator.Click();
        locator.SendKeys("0.0.0.0");

        driver.FindElement(By.ClassName("android.view.View")).Click();

        AppiumWebElement conectbtn = driver.FindElement(By.XPath("//android.widget.Button[@text='Connect']"));
        conectbtn.Click();

        Thread.Sleep(3000);
    }

}   

}

Here is the log:
[HTTP] Request idempotency key: 6f734930-2fc4-4612-83c0-33f6e498f440

[HTTP] --> POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element

[HTTP] {“using”:“id”,“value”:“com.android.packageinstaller:id/permission_allow_button”}

[W3C (01569cf9)] Calling AppiumDriver.findElement() with args: [“id”,“com.android.packageinstaller:id/permission_allow_button”,“01569cf9-4ba3-4716-b21c-0f707e3b1ee3”]

[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator

[BaseDriver] Waiting up to 0 ms for condition

[WD Proxy] Matched ‘/element’ to command name ‘findElement’

[WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8200/wd/hub/session/b8591a76-2403-4fed-a82b-8f624508084a/element] with body: {“strategy”:“id”,“selector”:“com.android.packageinstaller:id/permission_allow_button”,“context”:"",“multiple”:false}

[WD Proxy] Got response with status 200: {“sessionId”:“b8591a76-2403-4fed-a82b-8f624508084a”,“value”:{“ELEMENT”:“446aba33-4472-4f2d-af67-c11d8ed1545d”,“element-6066-11e4-a52e-4f735466cecf”:“446aba33-4472-4f2d-af67-c11d8ed1545d”}}

[W3C (01569cf9)] Responding to client with driver.findElement() result: {“element-6066-11e4-a52e-4f735466cecf”:“446aba33-4472-4f2d-af67-c11d8ed1545d”,“ELEMENT”:“446aba33-4472-4f2d-af67-c11d8ed1545d”}

[HTTP] <-- POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element 200 257 ms - 137

[HTTP]

[HTTP] Request idempotency key: dabb2504-6686-4fa8-a268-6d64622d7f47

[HTTP] --> POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element/446aba33-4472-4f2d-af67-c11d8ed1545d/click

[HTTP] {}

[W3C (01569cf9)] Calling AppiumDriver.click() with args: [“446aba33-4472-4f2d-af67-c11d8ed1545d”,“01569cf9-4ba3-4716-b21c-0f707e3b1ee3”]

[WD Proxy] Matched ‘/element/446aba33-4472-4f2d-af67-c11d8ed1545d/click’ to command name ‘click’

[WD Proxy] Proxying [POST /element/446aba33-4472-4f2d-af67-c11d8ed1545d/click] to [POST http://127.0.0.1:8200/wd/hub/session/b8591a76-2403-4fed-a82b-8f624508084a/element/446aba33-4472-4f2d-af67-c11d8ed1545d/click] with body: {“element”:“446aba33-4472-4f2d-af67-c11d8ed1545d”}

[WD Proxy] Got response with status 200: {“sessionId”:“b8591a76-2403-4fed-a82b-8f624508084a”,“value”:null}

[W3C (01569cf9)] Responding to client with driver.click() result: null

[HTTP] <-- POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element/446aba33-4472-4f2d-af67-c11d8ed1545d/click 200 136 ms - 14

[HTTP]

[HTTP] Request idempotency key: 24810a5a-cf28-4042-93b1-4c77c46c9ece

[HTTP] --> POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element

[HTTP] {“using”:“id”,“value”:“usernameBox”}

[W3C (01569cf9)] Calling AppiumDriver.findElement() with args: [“id”,“usernameBox”,“01569cf9-4ba3-4716-b21c-0f707e3b1ee3”]

[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator

[BaseDriver] Waiting up to 0 ms for condition

[WD Proxy] Matched ‘/element’ to command name ‘findElement’

[WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8200/wd/hub/session/b8591a76-2403-4fed-a82b-8f624508084a/element] with body: {“strategy”:“id”,“selector”:“usernameBox”,“context”:"",“multiple”:false}

[WD Proxy] Got response with status 404: {“sessionId”:“b8591a76-2403-4fed-a82b-8f624508084a”,“value”:{“error”:“no such element”,“message”:“An element could not be located on the page using the given search parameters”,“stacktrace”:"io.appium.uiautomator2.common.exceptions.ElementNotFoundException: An element could not be located on the page using the given search parameters\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:70)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:261)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:255)\n\tat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)\n\tat io.netty.chann…

[W3C] Matched W3C error code ‘no such element’ to NoSuchElementError

[W3C (01569cf9)] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.

[W3C (01569cf9)] at AndroidUiautomator2Driver.findElOrEls (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\lib\commands\find.js:75:11)

[W3C (01569cf9)] at runMicrotasks ()

[W3C (01569cf9)] at processTicksAndRejections (internal/process/task_queues.js:85:5)

[HTTP] <-- POST /wd/hub/session/01569cf9-4ba3-4716-b21c-0f707e3b1ee3/element 404 207 ms - 530

Kindly if anyone can help me with this.

What does this element look like in Inspector?

Ok, so what I’m seeing is that you click on some permissions button and then this screen renders. Could be that Appium is trying to find this element too fast. Could you try an ‘explicit’ wait? Waits explained here (with code examples):

I did use explicit wait, but got the same error. I also try implicit wait as well.

instead to use AppiumWebElement, you can prove to use Android driver

for example:
driver.findElement(AppiumBy.accessibilityId(“usernameBox”)).click();
where driver is:
public AndroidDriver driver;
AppiumDriverLocalService service;
UiAutomator2Options options = new UiAutomator2Options();

			options.setDeviceName(prop.getProperty("AndroidDeviceName"));
			options.setAutomationName("UiAutomator2");
            .
            .

driver = new AndroidDriver(service.getUrl(), options); // where with getUrl we move on http://127.0.0.1:4723

I’m not able to use AppiumBy.Id. I got errors in my code. Can you tell me any other way? And I also try with Android Driver but still have the same error.

have you import this ?

import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumBy.ByAccessibilityId;

Giuma, I’m using C# langauge

uhm sorry i didnt saw

https://stackoverflow.com/questions/56774116/could-not-access-element-by-id
https://stackoverflow.com/questions/76796123/not-able-to-use-find-element-by-id-on-browser-stack-using-appium-2-0

According to this, I should use AccessibilityId but in my scenario I don’t have accessibilityId rather I just have the Id.

try UiSelector

driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"usernameBox\")")) .click();
can you use AppiumBy on c#?

No, I can’t use AppiumBy in C#.