@AndroidFindBy annotation throwing error Element Not Visible Exception. I can find the element directly using the driver.findElementByXPath("//*[@id='SQ_LOGIN_USEARNAME']").sendKeys("Dqan");

Hi All,

I am new to Appium and not able to find element by @AndroidFindBy but I can find elements using the
“driver.findElementByXPath(”//*[@id=‘SQ_LOGIN_USEARNAME’]").sendKeys(“xyz”);". I have provided code for Object Page, Test Case and Driver class.

Driver Class

package com.xyz.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.annotations.BeforeTest;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeSuite;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;

public class MobileTestBase {

public static Properties prop; 	
public static AndroidDriver<AndroidElement> driver;	
private static final Logger logger = Logger.getLogger(MobileTestBase.class.getName());





public static void initiliaseAndroidDriver() throws MalformedURLException, InterruptedException {
	
	DesiredCapabilities cap = new DesiredCapabilities();
	cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
	cap.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
	driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
	driver = new AndroidDriver<AndroidElement>(cap);
}

Test Case:

package com.xzy.mobile.testcases;

import java.net.MalformedURLException;

import org.apache.log4j.Logger;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.xyz.base.MobileTestBase;

import com.mobile.xyz.pages.AndroidDriverObject;

import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class TestCaseAndroidDriver extends MobileTestBase{
private static final Logger logger = Logger.getLogger(MobileTestBase.class.getName());
public AndroidDriverObject androidPage;

@BeforeTest
public void setUp() throws InterruptedException, MalformedURLException{
loadConfigFile();
startServer();
//loadlog4J();

initiliaseAndroidDriver();
PageFactory.initElements(new AppiumFieldDecorator(driver),this);
androidPage = new AndroidDriverObject(driver); 
}


@Test
public void loginTest() throws InterruptedException {
	System.out.println("Logger Started");
	logger.info(" Page Opened");
	driver.get(prop.getProperty("url"));
	logger.info("Page Opened");
	Thread.sleep(2000);
	androidPage.username.sendKeys("Tester");
	logger.info("Send Data directly");
    driver.findElementByXPath("//*[@id='SQ_LOGIN_USERNAME']").sendKeys("Dqan");
	androidPage.login(prop.getProperty("username"), prop.getProperty("password"));

}

}

Page Object

package com.mobile.xyz.pages;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.xyz.base.MobileTestBase;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class AndroidDriverObject extends MobileTestBase{

// AndroidDriverObject androidDri;

@AndroidFindBy(xpath="//*[@id='SQ_LOGIN_USERNAME']")
public WebElement username;

@AndroidFindBy(xpath="//*[@name='SQ_LOGIN_PASSWORD']")
public WebElement password;

@AndroidFindBy(xpath="//input[@name='log_in_out_button']")
public WebElement loginBtn;




public AndroidDriverObject(WebDriver driver) {
	
	PageFactory.initElements(driver, this);
	PageFactory.initElements(new AppiumFieldDecorator(driver,
			10, //default implicit waiting timeout for all strategies
			TimeUnit.SECONDS),
			this
			);
}
	

//Function with String variables
public void login(String un, String pwd){
	System.out.println("Login Test Reached");
	System.out.println(username.isDisplayed()+username.getTagName()+"Username is displayed");
	
	username.sendKeys(un);
	password.sendKeys(pwd);
	loginBtn.click();
	
}

}

Thanks in advance,
Raj

Any updates regarding this issue

Xpath for me works best with full resource id. (Including class)
And why are you using xpath for @AndroidFindBy if you can use id = “id” ?