说下我切换的方法:
先去查微信使用 webview 的版本:

WebView in com.tencent.mm (57.0.2987.132)

从 chromedriver 中查询到:

----------ChromeDriver v2.28 (2017-03-09)----------
Supports Chrome v55-57

Error: chromedriver proxy error with: Error: connect ECONNREFUSED 127.0.0.1:9522
    at Request.request [as _callback] (/usr/local/lib/node_modules/macaca-android/node_modules/_macaca-chromedriver@1.0.40@macaca-chromedriver/lib/proxy.js:57:25)
    at self.callback (/usr/local/lib/node_modules/macaca-android/node_modules/_request@2.69.0@request/request.js:199:22)
    at emitOne (events.js:115:13)
    at Request.emit (events.js:210:7)
    at Request.onRequestError (/usr/local/lib/node_modules/macaca-android/node_modules/_request@2.69.0@request/request.js:821:8)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at Socket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
>> proxy.js:51:14 [master] pid:34744 Proxy: /status:GET to http://localhost:9522/wd/hub/status:GET with body: 
>> proxy.js:59:16 [master] pid:34744 Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Mac OS X\",\"version\":\"10.12.6\"}}}"
>> proxy.js:51:14 [master] pid:34744 Proxy: /session:POST to http://localhost:9522/wd/hub/session:POST with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidDeviceSerial":"587c1673","androidProcess":"com.tencent.mm:tools"}}}
>> proxy.js:59:16 [master] pid:34744 Got response with status 200: {"sessionId":"94d4623108893e1c7e97b2069357da6b","status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.28.455517 (2c6d2707d8ea850c862f04ac066724273981e88f),platform=M...
>> chromedriver ready with: {"sessionId":null,"status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.28.455517 (2c6d2707d8ea850c862f04ac066724273981e88f),platform=Mac OS X 10.12.6 x86_64)"}}

代码如下:

@BeforeClass
    public void setup() {
        try {
            JSONObject porps = new JSONObject();
            porps.put("autoAcceptAlerts", true);
//            porps.put("platformVersion", "5.1");
//            porps.put("deviceName", "MI5");
            porps.put("platformName", "android");
            porps.put("reuse", 3);
            porps.put("package", "com.tencent.mm");
            porps.put("activity", ".ui.LauncherUI");
            // 关键是加上这段
            porps.put("androidProcess", "com.tencent.mm:tools");
            JSONObject desiredCapabilities = new JSONObject();
            desiredCapabilities.put("desiredCapabilities", porps);
            driver.initDriver(desiredCapabilities);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    @Test
    public void StartExan() throws Exception {

        try {
            setup();
            driver.sleep(2000);
            driver.elementByXPath("//*[@text='发现']").click();
            driver.elementByXPath("//*[@text='小程序']").click();
            driver.sleep(2000);
            driver.elementByXPath("//*[@text='美团外卖+']").click();
            Thread.sleep(5000);
            JSONArray a = driver.contexts();

            driver.context(driver.contexts().getString(2));//进入小程序后会有3个web context,其中第二个是真正的小程序webview
            Thread.sleep(5000);
            System.out.println(driver.elementByClassName("poi-list-header").getText());//输出美团外卖+小程序首页的“附近商家”
            Thread.sleep(2000);
            System.out.println(driver.currentContext());
            driver.quit();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

看了 github 上面说是版本问题,也试过很多 2.22-2.30 都报这个问题,帮忙了解的人看看。


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