appium 二次切换webview后无法找到页面元素

问题:在使用Appium进行android自动化测试时,第一次切换webview可以正常找到元素,但切换到NATIVE_APP后,再次切换到webview时,appium就无法定位元素,且等待一段时间后,自动退出执行。

原因为:Appium第一次切换到Html页面时,会新生成一个Chromedriver;当第二次切换到Html时,会使用已经存在的Chromedriver。

解决方式:

  找到AppiumAppium ode_modulesappiumlibdevicesandroid目录下的android-hybrid.js文件,找到如下代码

androidHybrid.startChromedriverProxy = function (context, cb) {
  cb = _.once(cb);
  logger.debug("Connecting to chrome-backed webview");
  if (this.chromedriver !== null) {
    return cb(new Error("We already have a chromedriver instance running"));
  }

  if (this.sessionChromedrivers[context]) {
      //in the case where we've already set up a chromedriver for a context,'
      //we want to reconnect to it, not create a whole new one
      this.setupExistingChromedriver(context, cb);
    } else {
      this.setupNewChromedriver(context, cb);
    }
   
};

  

然后,修改如下:

androidHybrid.startChromedriverProxy = function (context, cb) {
  cb = _.once(cb);
  logger.debug("Connecting to chrome-backed webview");
  if (this.chromedriver !== null) {
    return cb(new Error("We already have a chromedriver instance running"));
  }

  // 修改时间:2018-07-23  目的:二次切换webview后,chromeDriver未重新生成,无法定位元素
  // if (this.sessionChromedrivers[context]) {
  //   // in the case where we've already set up a chromedriver for a context,
  //   // we want to reconnect to it, not create a whole new one
  //   this.setupExistingChromedriver(context, cb);
  // } else {
   //   this.setupNewChromedriver(context, cb);
   // }
   this.setupNewChromedriver(context,cb);
  // 修改时间:2018-07-23  目的:二次切换webview后,chromeDriver未重新生成,无法定位元素
};
原文地址:https://www.cnblogs.com/JcHome/p/9429717.html