文集 移動(dòng)端網(wǎng)頁(yè)端爬蟲(chóng)
單設(shè)備自動(dòng)化
-
準(zhǔn)備
假設(shè)軟件環(huán)境都已配置
一臺(tái)android虛擬機(jī)忿偷, 我的是夜神端口是62025
-
啟動(dòng)
-
連接設(shè)備
[圖片上傳失敗...(image-cd54b7-1532398731012)]
運(yùn)行appium材蛛,默認(rèn)啟動(dòng)4723端口
[圖片上傳失敗...(image-72a668-1532398731012)]
備注:
? 1) 設(shè)備提前裝好app, appActivity和appPackage的值下面介紹
? 2)deviceName: 如果是真機(jī)走哺,填設(shè)備的型號(hào)(如我的華為暢享7: SLA-LA00)
-
審查元素
[圖片上傳失敗...(image-cc0a5d-1532398731012)]
上圖已經(jīng)標(biāo)記出了appPackage蚯嫌,至于appActivity指的是android app的入口activity組件,默認(rèn)是MainActivity丙躏,可以通過(guò)反編譯工(AndroidKiller)具查看择示。
-
capability設(shè)置
{ "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()