# coding=utf-8
from appium import webdriver
from time import sleep
def get_driver():
capabilities = {
"platformName": "android",
"deviceName": "192.168.56.101:5555",
"app": "/Users/francis/Projects/Appium/app_package/release.apk_1.2.1.apk",
# "appActivity": "com.rootoken.wallet.MainActivity",
"noReset": "true"
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities)
sleep(2)
return driver
def window_handle():
# 窗口切换到新打开的窗口
c_window = driver.current_context
print(c_window)
all_handles = driver.contexts
for handle in all_handles:
print(handle)
if 'WEBVIEW' in handle:
driver.switch_to.context(handle)
print('跳转到webview模式')
print('跳转当前窗口句柄:' + handle)
break
def get_size():
size = driver.get_window_size()
width = size['width']
height = size['height']
return width, height
def swipe_left():
size = get_size()
x1 = size[0]*9/10
y1 = size[1]/2
x = size[0]/10
y = y1
driver.swipe(x1, y1, x, y)
def swipe_right():
size = get_size()
x1 = size[0]/10
y1 = size[1]/2
x = size[0]*9/10
y = y1
driver.swipe(x1, y1, x, y)
def swipe_up():
size = get_size()
x1 = size[0]/2
y1 = size[1]*9/10
x = x1
y = size[1]/10
driver.swipe(x1, y1, x, y)
def swipe_down():
size = get_size()
x1 = size[0]/2
y1 = size[1]/10
x = x1
y = size[1]*9/10
driver.swipe(x1, y1, x, y)
def swipe_on(direction):
if direction == 'up':
swipe_up()
elif direction == 'down':
swipe_down()
elif direction == 'left':
swipe_left()
else:
swipe_right()
driver = get_driver()
# # language_choice = '//android.widget.Button[contains(@content-desc, "简体中文")]'
# language_choice = '简体中文'
# sleep(3)
# driver.find_element_by_accessibility_id(language_choice).click()
sleep(3)
swipe_on('left')
swipe_on('left')
swipe_on('left')
sleep(2)
window_handle()
# step_in = driver.find_elements_by_class_name('android.widget.Button')
# step_in[1].click()
# driver.find_element_by_link_text('进入ROOTOKEN').click()
日志:
[debug] [ADB] Running '/Users/francis/Downloads/APPIUM/android-sdk-macosx/platform-tools/adb -P 5037 -s 192.168.56.101\:5555 shell dumpsys package com.android.chrome'
[Chromedriver] Unable to discover Chrome version. Using Chromedriver 2.37 at '/usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac/chromedriver'
[Chromedriver] Set chromedriver binary as: /usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac/chromedriver
[debug] [Chromedriver] Killing any old chromedrivers, running: pkill -15 -f "/usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac/chromedriver.*--port=8000"
[Chromedriver] No old chromedrivers seemed to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running '/Users/francis/Downloads/APPIUM/android-sdk-macosx/platform-tools/adb -P 5037 -s 192.168.56.101\:5555 forward --list'
[debug] [ADB] Removing forwarded port socket connection: 12872
[debug] [ADB] Running '/Users/francis/Downloads/APPIUM/android-sdk-macosx/platform-tools/adb -P 5037 -s 192.168.56.101\:5555 forward --remove tcp\:12872'
[Chromedriver] Spawning chromedriver with: /usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac/chromedriver --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.37.544337'
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Mac OS X\",\"version\":\"10.11.6\"}}}"
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.rootoken.wallet","androidUseRunningApp":true,"androidDeviceSerial":"192.168.56.101:5555"}}}
[debug] [Chromedriver] Webview version: 'Chrome/51.0.2704.91'