pycharm 报错信息
selenium.common.exceptions.SessionNotCreatedException: Message: A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c “E:\android-sdk-windows\platform-tools\adb.exe -s 02743107C5002344 shell “ps ‘uiautomator’ bad pid ‘uiautomator’)
原因:
对应执行的指令是ps ‘uiautomator’,Android7不支持这个指令格式,所以执行结果是bad pid ‘uiautomator’;
目前Appium未对此进行处理,所以需要修改此指令的执行方式。
解决方法:
1、从目录Appium的安装目录\Appium\node_modules\appium\node_modules\appium-adb\lib找到文件adb.js。
2、修改代码:
ADB.prototype.shell = function (cmd, cb) {
if (cmd.indexOf(‘"‘) === -1) {
cmd = ‘"‘ + cmd + ‘"‘;
}
var execCmd = ‘shell ‘ + cmd;
this.exec(execCmd, cb);
};
在上面的代码段下方添加代码段:
ADB.prototype.shell_grep = function (cmd, grep, cb){
if (cmd.indexOf(‘"‘) == -1){
cmd = ‘"‘ + cmd + ‘"‘;
}
var execCmd = ‘shell‘ + cmd + ‘| grep ‘ + grep;
this.exec(execCmd, cb);
};
修改代码:
ADB.prototype.getPIDsByName = function (name, cb) {
logger.debug("Getting all processes with ‘" + name + "‘");
this.shell("ps ‘" + name + "‘", function (err, stdout) {
if (err) return cb(err);
stdout = stdout.trim();
var procs = [];
var outlines = stdout.split("\n");
_.each(outlines, function (outline) {
if (outline.indexOf(name) !== -1) {
procs.push(outline);
}
});
if (procs.length < 1) {
logger.debug("No matching processes found");
return cb(null, []);
}
var pids = [];
_.each(procs, function (proc) {
var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);
if (match) {
pids.push(parseInt(match[1], 10));
}
});
if (pids.length !== procs.length) {
var msg = "Could not extract PIDs from ps output. PIDS: " +
JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);
return cb(new Error(msg));
}
cb(null, pids);
});
};
将上方代码段注释掉,新增下方的代码段:
ADB.prototype.getPIDsByName = function (name, cb) {
logger.debug("Getting all processes with ‘" + name + "‘");
this.shell_grep("ps", name, function (err, stdout) {
if (err) {
logger.debug("No matching processes found");
return cb(null, []);
}
var pids = [];
_.each(procs, function (proc) {
var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);
if (match) {
pids.push(parseInt(match[1], 10));
}
});
if (pids.length !== procs.length) {
var msg = "Could not extract PIDs from ps output. PIDS: " +
JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);
return cb(new Error(msg));
}
cb(null, pids);
});
};
注意:注释记得要用 // 进行注释
最近学习appium 碰到这个问题,在网上找了好久,这个方法是在这个大神博客看见的,原文链接:https://blog.csdn.net/xiaocai5731/article/details/104314886