Appium 自己写了一个登录 qq 的程序,可是登录时的状态有 3 种,自己只会写 2 种,求大神指点下 (折磨自己 2 天了)

littlefive · August 05, 2018 · Last by huan replied at August 06, 2018 · 1575 hits
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException

import time

desired_caps = {}
desired_caps['platformName'] = 'android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = '127.0.0.1:7555'
desired_caps["unicodeKeyboard"] = 'True'  #声明中文
desired_caps["resetKeyboard"] = 'True'  #声明中文,否则不支持中文
desired_caps['noReset'] = 'True' #执行时不初始化
desired_caps['appPackage'] = 'com.tencent.mobileqq'
desired_caps['appActivity'] = '.activity.SplashActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
time.sleep(2)

#第一次登录、第二次登录有数据记录、第三次登录,有帐号数据但未登录

def login():
    driver.find_element_by_xpath("//android.widget.EditText[@text='QQ号/手机号/邮箱' and @content-desc='请输入QQ号码或手机或邮箱']").clear()
    driver.find_element_by_xpath("//android.widget.EditText[@text='QQ号/手机号/邮箱' and @content-desc='请输入QQ号码或手机或邮箱']").click()
    driver.find_element_by_xpath("//android.widget.EditText[@text='QQ号/手机号/邮箱' and @content-desc='请输入QQ号码或手机或邮箱']").send_keys("331089688")
    time.sleep(3)
    driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tencent.mobileqq:id/password']").clear()
    driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tencent.mobileqq:id/password']").click()
    driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tencent.mobileqq:id/password']").send_keys("xiaowu123123")
    time.sleep(3)
    driver.find_element_by_xpath("//android.widget.Button[@resource-id='com.tencent.mobileqq:id/login']").click()
    time.sleep(2)

try:
    driver.find_element_by_xpath("//android.widget.Button[@resource-id='com.tencent.mobileqq:id/btn_register']")  #检测是否有这个控件
except NoSuchElementException:           #检测结果:无。有异常,执行语句
    login()
    time.sleep(3)
    print("自动登录成功")
else:                                                                   #检测结果:有。无异常,则执行该语句
    driver.find_element_by_xpath("//android.widget.Button[@resource-id='com.tencent.mobileqq:id/btn_login']").click()
    time.sleep(3)
    login()
    print("输入帐号密码成功")                    #2种方式,目前第三种带帐号密码还不知道如何写入

有 3 种情况:
1,第一次登录,需要点击登录,然后再输入帐号密码登录

2,登录过,不会出现新用户,但是要输入帐号密码

3,直接登录进入,不需要输入帐号密码

try except else
目前我只能满足 2 种情况,想不明白怎么区别这 3 种情况?求大神指点下。

要么满足 1 和 2 种情况,要么满足 2 和 3 的情况,要么满足 1 和 3 的情况。请问如何要满足以上 3 种情况。。。真心求教。。

共收到 10 条回复 时间 点赞

三种情况写到三个 case 呀

写一个等待\查看元素的方法
启动 activity 后判断一下,三个页面不同的元素,找到哪个元素就走哪个 case

Author only
匿名 #4 · August 06, 2018

一不小心看到了 331089688 密码 xiaowu123123 😅

雨夜狂奔 回复

额。。。写在一起撒。。不能每次启动 3 次。。

皆非 回复

求指点。。。搞的头疼了。。。。脑子没有转过来这个弯。。。

ola嘿嘿 回复

想知道用什么方法。。例如:try expect else?还是什么。

同学,你关注点可不对哦。。

littlefive 回复
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as WT

查看这个方法

用 if 不就行了? 首判断是不是自动登录了,再判断有没有新用户这个关键字或 id 之类的,不就可以了

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up