Why cant we use by.id method for locating elements!

Hello,

I am trying to use by.id way for locating elements on facebook android app ,Why cant we use this script for identifying the ui elements of facebook android app as i when i run the given below script i get error as
Exception in thread “main” java.lang.Error: Unresolved compilation problem:

at com.appium.facebook.androiduiselector.Logintest2.main(Logintest2.java:11)

This is my script:-

import java.net.URL;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Logintest2 {
public static void main(String[] args)
{

    DesiredCapabilities capabilities = new DesiredCapabilities() ;
	capabilities.setCapability("automationName","Appium");
	capabilities.setCapability("platformnName","Android");
	capabilities.setCapability("platformVersion","5.1.1");
	capabilities.setCapability("deviceName","Nexus 6" );
	capabilities.setCapability("app","C:\\Users\\Jenny\\Downloads\\facebook.apk");
	capabilities.setCapability("appPackage","com.facebook.katana");
	capabilities.setCapability("appActivity", "com.facebook.katana.LoginActivity");

	AndroidDriver driver = new AndroidDriver (new URL ("http://127.0.0.1:4723/wd/hub"), capabilities) ;

driver.findElement(By.id(“com.facebook.katana:id/login_username”)).sendKeys("[email protected]");
driver.findElement(By.id(“com.facebook.katana:id/login_password”)).sendKeys(“xxxxxx”);
driver.findElement(By.id(“com.facebook.katana:id/login_login”)).click();

Thread.sleep(3000);
driver.quit();
}

}

Remove the “com.facebook.katana:id/” then it should work.

HI @Rjimms,

Is it prefered to give resource id as By.id(“login_username”) with out package name as mentioned before.?

Note: It is working when i’m giving id along with combination of package name.

Please let me know !!!

Thanks,
Bhaskar.

Personally I prefer to leave the package name out but It’ll be up to you.

I also prefer to use Page Object Model - http://www.toolsqa.com/selenium-webdriver/page-object-model/ - as it improves readability as well as reducing code re-use.