# 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()
route_in = "//android.widget.Button[contains(@content-desc, '进入')]"
driver.find_element_by_xpath(route_in).click()
日志:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//android.widget.Button[contains(@content-desc, '进入')]"}
(Session info: chrome=51.0.2704.91)
(Driver info: chromedriver=2.23.409710 (0c4084804897ac45b5ff65a690ec6583b97225c0),platform=Mac OS X 10.11.6 x86_64)