移动端爬虫--单设备自动化(二)
2018-07-24 本文已影响0人
独占潇洒_b556
文集 移动端网页端爬虫
单设备自动化
-
准备
假设软件环境都已配置
一台android虚拟机, 我的是夜神端口是62025
-
启动
-
连接设备
[图片上传失败...(image-cd54b7-1532398731012)]
-
运行appium,默认启动4723端口
[图片上传失败...(image-72a668-1532398731012)]
备注:
1) 设备提前装好app, appActivity和appPackage的值下面介绍
2)deviceName: 如果是真机,填设备的型号(如我的华为畅享7: SLA-LA00)
-
审查元素
[图片上传失败...(image-cc0a5d-1532398731012)]
上图已经标记出了appPackage,至于appActivity指的是android app的入口activity组件,默认是MainActivity,可以通过反编译工(AndroidKiller)具查看。
-
capability设置
{ "platformName": "Android", "automationName": "Appium", "platformVersion": "5.1.1", "appPackage": "com.ss.android.ugc.aweme", "appActivity": "com.ss.android.ugc.aweme.main.MainActivity", "noReset": "True", "deviceName": "Emulator" }
-
到此appium操控app成功。下面给出一段代码,使用python操控
# -*- coding: utf-8 -*-
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.touch_action import TouchAction
# the emulator is sometimes slow and needs time to think
SLEEPY_TIME = 1
desired_caps = {
"platformName": "Android",
"automationName": "Appium",
"platformVersion": "5.1.1",
"appPackage": "com.ss.android.ugc.aweme",
"appActivity": "com.ss.android.ugc.aweme.main.MainActivity",
"noReset": "True",
"deviceName": "Emulator"
}
class App(object):
def __init__(self):
self.driver = webdriver.Remote('http://%s:%d/wd/hub' % ('127.0.0.1', 4723), desired_caps)
self.wait = WebDriverWait(self.driver, 30)
# {'width':w, 'height':h}
self.window = self.driver.get_window_size()
def set_up(self):
id = self.driver.session_id
print(id)
def tear_down(self):
self.driver.quit()
def wait_element_clickable(self, locator):
return self.wait.until(
EC.element_to_be_clickable(locator)
)
def back(self):
self.driver.back()
def center(self, left_top, right_bottom):
x = (left_top[0] + right_bottom[0]) / 2
y = (left_top[1] + right_bottom[1]) / 2
return x, y
def click_by_xy(self, left_top, right_bottom):
location = self.center(left_top, right_bottom)
self.driver.tap([location], 200)
def swip(self, x1, y1, x2, y2, duration=None):
action = TouchAction(self.driver)
action.press(x=x1, y=y1) \
.wait(300) \
.move_to(x=x2, y=y2) \
.release() \
.perform()
def app_is_installed(self, app_package):
self.driver.is_app_installed(app_package)
@property
def page_source(self):
return self.driver.page_source
def quit(self):
self.driver.quit()
if '__main__' == __name__:
t = App()