Search code examples
seleniumappium

Appium can not operate the element


I'm using below code to test an Android App with Appium, but when I try to operate the element

driver.findElementById("com.TdMobile.ipems_app:id/login_ipaddress_edit").sendKeys("http://192.168.0.71:8080/ipems/");   

I got java.lang.NullPointerException, when I debug into the line, I find the element is not null, does anyone know this happened?

AndroidDriver driver = null;
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("automationName", "Appium");//appium做自动化
cap.setCapability("app", "C:\\Users\\baiming.zhang\\Desktop\\IPEMS-APP.apk");
cap.setCapability("deviceName", "xiaomi-mi_5");//设备名称
cap.setCapability("platformName", "Android"); //安卓自动化还是IOS自动化
cap.setCapability("platformVersion", "6.0"); //安卓操作系统版本
cap.setCapability("udid", "794b155c"); //设备的udid (adb devices 查看到的) 
cap.setCapability("unicodeKeyboard", "True"); //支持中文输入
cap.setCapability("resetKeyboard", "True"); //支持中文输入,必须两条都配置
cap.setCapability("noSign", "True"); //不重新签名apk
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"),cap);//把以上配置传到appium服务端并连接手机
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//隐式等待

driver.findElementById("com.TdMobile.ipems_app:id/login_ipaddress_edit").sendKeys("http://192.168.0.71:8080/ipems/");//this line failed.
driver.findElementById("com.TdMobile.ipems_app:id/login_user_edit").sendKeys("http://192.168.0.71:8080/ipems/");
driver.findElementById("com.TdMobile.ipems_app:id/login_passwd_edit").sendKeys("http://192.168.0.71:8080/ipems/");
driver.findElementById("com.TdMobile.ipems_app:id/login_login_btn").click();

The complete stacktrace is:

Exception in thread "main" java.lang.NullPointerException at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275) at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:316) at simple.CalculatorTest.main(CalculatorTest.java:47)


Solution

  • I changed AndroidDriver to RemoteWebDriver, than it works fine for me