源代码如下:
/**

//import org.testng.annotations.Test;
//import org.testng.annotations.BeforeClass;
//import org.testng.annotations.AfterClass;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;

public class login {

// private AppiumDriver driver;

//public void setup() throws Exception

public static void main(String[] args) throws MalformedURLException, InterruptedException{

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("platformName", "Android"); //指定测试平台
cap.setCapability("deviceName", "2e65be33"); //指定测试机的 ID,通过 adb 命令adb devices获取
cap.setCapability("platformVersion", "6.1");
//cap.setCapability("platformVersion", "4.4");
cap.setCapability("device", "Android");
cap.setCapability("automationName", "Appium");

//将上面获取到的包名和 Activity 名设置为值
cap.setCapability("appPackage", "com.bluestar.doctor");
// cap.setCapability("appActivity", "com.bluestar.doctor.login.LoginActivity");

// cap.setCapability("appPackage", "com.miui.home");
cap.setCapability("appActivity", ".login.SplashActivity");

//A new session could not be created 的解决方法
// cap.setCapability("appWaitActivity","com.bluestar.doctor.login.LoginActivity");
cap.setCapability("appWaitActivity",".login.SplashActivity");
//每次启动时覆盖 session,否则第二次后运行会报错不能新建 session
cap.setCapability("sessionOverride", true);

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

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

// AndroidDriver driver = webdriver.Remote('http://127.0.0.1:4726/wd/hub', desired_caps);
// AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

Thread.sleep(10000);

//driver1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//登录操作

driver1.findElementById("com.bluestar.doctor:id/et_username").clear();
driver1.findElementById("com.bluestar.doctor:id/et_username").sendKeys("420323197712179231");
driver1.findElementById("com.bluestar.doctor:id/et_pwd").clear();
driver1.findElementById("com.bluestar.doctor:id/et_pwd").sendKeys("123456");
driver1.findElementById("com.bluestar.doctor:id/btn_login").click();

driver1.quit();

}

}

在 Eclipse 中运行如下:
九月 10, 2018 8:08:33 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {ELEMENT=1}
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: '6921Y1O05X5QTK6', ip: '172.31.252.191', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181'
Driver info: driver.version: AndroidDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:417)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:70)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
at com.example.login.login.main(login.java:75)
Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to org.openqa.selenium.WebElement
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:415)
... 8 more


↙↙↙阅读原文可查看相关链接,并与作者交流