終極利器!利用appium和mitmproxy登入獲取cookies
阿新 • • 發佈:2018-11-11
環境搭建
參考我之前寫的https://www.cnblogs.com/c-x-a/p/9163221.html
appium
程式碼start_appium.py
# -*- coding: utf-8 -*-
# @Time : 2018/10/8 11:00
# @Author : cxa
# @File : test.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
sitename = 'ali'
def response(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