环境搭建
参考我之前写的windows中Appium-desktop配合夜神模拟器的使用
appium
代码start_appium.py
# -*- coding: utf-8 -*-
# @Time : 2018/10/8 11:00
# @Author : cxa
# @File : start_appium.py
# @Software: PyCharmctx
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import base64
def start_appium():
desired_caps = {}
desired_caps['platformName'] = 'Android' # 设备系统
desired_caps['deviceName'] = '127.0.0.1:62001' # 设备名称
desired_caps['appPackage'] = 'com.xxxx.xxxx' # 测试app包名,如何获取包名方式看上面的环境搭建。
desired_caps['appActivity'] = 'com.xxxx.xxxx.xxx.xxxx' # 测试appActivity,如何获取包名方式看上面的环境搭建。
desired_caps['platformVersion'] = '4.4.2' # 设备系统的安卓版本,版本不要太高,设计安全策略得外部因素。
desired_caps['noReset'] = True # 启动后结束后不清空应用数据
desired_caps['unicodeKeyboard'] = True # 此两行是为了解决字符输入不正确的问题
desired_caps['resetKeyboard'] = True # 运行完成后重置软键盘的状态
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 启动app,启动前记得打开appium服务。
wait = WebDriverWait(driver, 60)#设置等待事件
try:
btn_xpath = '//android.widget.Button[@resource-id="com.alicom.smartdail:id/m_nonum_confirm_btn"]'
btn_node = wait.until(EC.presence_of_element_located((By.XPATH, btn_xpath)))#等元素出现再继续,最长等待时间上面设置的60s。
# btn_node=driver.find_element_by_xpath(btn_xpath)
btn_node.click()
except:
driver.back()
btn_xpath = '//android.widget.Button[@resource-id="com.alicom.smartdail:id/m_nonum_confirm_btn"]'
btn_node = wait.until(EC.presence_of_element_located((By.XPATH, btn_xpath)))
# btn_node = driver.find_element_by_xpath(btn_xpath)
btn_node.click()
# sleep 30s
# 点击
def login_in(driver):
id_xpath = '//android.widget.EditText[@content-desc="账户名输入框"]'
id_node = driver.find_element_by_xpath(id_xpath)
id_node.clear()
id_node.send_keys("test")
pwd = str(base64.b64decode("MTIzNHF3ZXI="), 'u8')
pwd_xpath = '//android.widget.EditText[@content-desc="密码输入框"]'
pwd_node = driver.find_element_by_xpath(pwd_xpath)
pwd_node.clear()
pwd_node.send_keys(pwd)
submit = "//android.widget.Button[@text='登录']"
submit_node = driver.find_element_by_xpath(submit)
submit_node.click()
time.sleep(10)
if __name__ == '__main__':
start_appium()
mitmproxy
代码
mitm_proxy_script.py
# -*- coding: utf-8 -*-
# @Time : 2018/10/8 11:00
# @Author : cxa
# @File : mitm_proxy_script.py
# @Software: PyCharm
import sys
def request(flow):
request = flow.request
if '.png' in request.url or 'xxx.x.xxx.com' not in request.url:
return #如果不在观察的url内则返回
if 'xxx.x.xxx.com' in request .url:
print(request .url)
cookies = dict(request.cookies) #转换cookies格式为dict
if cookies:
save_cookies(repr(cookies))#如果不为空保存cookies
def save_cookies(cookies):
sys.path.append("../")
from database import getcookies
getcookies.insert_data(sitename, cookies) #保存cookies