appium 碰到的坑

1、failed install setting_apk-debug.apk
解决办法:
找到android.js 找到下面代码:
async.series([
this.initJavaVersion.bind(this),
this.initAdb.bind(this),
this.packageAndLaunchActivityFromManifest.bind(this),
this.initUiautomator.bind(this),
this.prepareDevice.bind(this),
this.checkApiLevel.bind(this),
this.pushStrings.bind(this),
this.processFromManifest.bind(this),
this.uninstallApp.bind(this),
this.installAppForTest.bind(this),
this.forwardPort.bind(this),
this.pushAppium.bind(this),
this.initUnicode.bind(this),
// this.pushSettingsApp.bind(this),
//this.pushUnlock.bind(this),
function (cb) {this.uiautomator.start(cb);}.bind(this),
this.wakeUp.bind(this),
this.unlock.bind(this),
this.getDataDir.bind(this),
注释掉:
this.pushSettingsApp.bind(this),
this.pushUnlock.bind(this),

2、连接真机报command failed shell “ps ‘uiautomator错误
文件路径为:/Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-adb/lib
找到一下代码:
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”);
outlines.shift();
_.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,问题终于解决。

senlenium send.key批量输入

#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Safari()
driver.get(“http://192.168.1.17:8090/login”)
time.sleep(3)
driver.find_element_by_id(“amount”).send_keys(“输入你的账号”)
driver.find_element_by_id(“password”).send_keys(“输入你的密码”)
driver.find_element_by_xpath(“/html/body/section/div/div[2]/div/form/input”).click()
time.sleep(5)
a= 0
while a<=3:

connect = ["test1","hi",u"地方","dhf"]
for i in connect:

driver.find_element_by_xpath("/html/body/header/div/div[1]/ul/li[3]").click()
time.sleep(5)
driver.find_element_by_class_name("add-shop").click()
time.sleep(3)
driver.find_element_by_tag_name("input").send_keys(i)
driver.find_element_by_class_name("btn btn-primary").click()
a+=1
time.sleep(4)
driver.quit()

selenium 获取登录cookie

#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Safari()
driver.get(“http://192.168.1.17:8090/login”)
time.sleep(3)
driver.find_element_by_id(“amount”).send_keys(“173”)
driver.find_element_by_id(“password”).send_keys(“123”)
driver.find_element_by_xpath(“/html/body/section/div/div[2]/div/form/input”).click()
time.sleep(5)
cookie = driver.get_cookies()
cookie_list = []
for i in cookie:
cookie = i[‘name’] + ‘=’ + i[‘value’]
cookie_list.append(cookie)
cookie_str = “;”.join(cookie_list)
print cookie_str
driver.quit()

selenium 登录Demo

#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Safari()
driver.get(“http://192.168.1.17:8090/login”)
time.sleep(3)
driver.find_element_by_id(“amount”).send_keys(“173”)
driver.find_element_by_id(“password”).send_keys(“123”)
driver.find_element_by_xpath(“/html/body/section/div/div[2]/div/form/input”).click()
time.sleep(4)
driver.quit()

selenium web页面滚动滚动条

#coding=utf-8
from selenium import webdriver
import  time


driver = webdriver.Safari()
driver.set_page_load_timeout(10)
driver.get('https://www.baidu.com/')
driver.maximize_window()

driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep(3)
js = "var c=document.body.scrollTop=100000"
driver.execute_script(js)
time.sleep(6)
driver.quit()

第二种:
#coding=utf-8
from selenium import webdriver
import time

driver = webdriver.Safari()
driver.set_page_load_timeout(10)
driver.get('https://www.baidu.com/')
driver.maximize_window()

driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.execute_script("window.scrollTo(0,100000)")
time.sleep(6)
driver.quit()

selenium 网页搜索Demo

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class PythonSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Safari()
    def test_search(self):
        driver = self.driver
        driver.get("http://www.python.org")
        elem = driver.find_element_by_name("q")
        elem.send_keys("pyhton")

        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source
    def tearDown(self):
        self.driver.quit()
if __name__ == "__main__":
    unittest.main()