没有用框架的代码实现登录功能:
import time
from selenium import webdriver
# 创建字典
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'm2 note'
desired_caps['appPackage'] = 'com.test.abc'
desired_caps['appActivity'] = 'com.app.ui.activity.WelcomeActivity'
# 方便输入中文
desired_caps['unicodeKeyboard'] = 'True'
desired_caps['resetKeyboard'] = 'True'
# 每次测试清空缓存
desired_caps['noReset'] = 'False'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(6)
# 通过xpath定位“去登录”按钮并点击
driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.TextView[2]').click()
# 输入用户名、密码,点击登录按钮
driver.find_element_by_xpath('/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText').send_keys('username')
driver.find_element_by_xpath('/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.EditText').send_keys('password')
driver.find_element_by_xpath('/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.Button').click()
# 判断是否成功
try:
if driver.find_element_by_xpath("test/test").is_displayed():
print('pass')
except Exception as e:
print(e)
print("fail")
time.sleep(3)
# 资源释放
driver.quit()