PO模式执行testcase报错 AttributeError: 'NoneType' object has no attribute 'find_element',对应发生报错的basepage如下

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from bathroom_page.comon.base_page import BasePage
from bathroom_page.main import Main

class App(BasePage):
#启动欢迎页"同意"按钮
agree_loc = (MobileBy.ID, ‘com.qekj.merchant:id/tv_know’)
#"立即进入"按钮
open_loc = (MobileBy.ID, ‘com.qekj.merchant:id/btn_open’)
#agree_title = “同意”
update_alter = (MobileBy.ID, ‘com.qekj.merchant:id/ll_update’)
#app首页更新弹窗的取消按钮
update_cancel = (MobileBy.ID, ‘com.qekj.merchant:id/iv_cancel’)
shouyi_page = (MobileBy.ID, ‘com.qekj.merchant:id/rl_shouyi’)
#清空登录账号框按钮
clear_count_button = (MobileBy.ID, ‘com.qekj.merchant:id/iv_clear_account’)
#清空登录密码框按钮
clear_psw_button = (MobileBy.ID, ‘com.qekj.merchant:id/iv_clear_password’)
#登录页面账号输入框
account_input = (MobileBy.ID, ‘com.qekj.merchant:id/et_account’)
#登录页面密码输入框
password_input = (MobileBy.ID, ‘com.qekj.merchant:id/et_password’)
#登录页面"同意"按钮
agree_button = (MobileBy.ID, ‘com.qekj.merchant:id/iv_select’)
#登录页面"登录"按钮
login_button = (MobileBy.ID, ‘com.qekj.merchant:id/rl_login’)
#清空登录账号框按钮
clear_count = (MobileBy.ID, ‘com.qekj.merchant:id/iv_clear_account’)
#清空登录密码框按钮
clear_psw = (MobileBy.ID, ‘com.qekj.merchant:id/iv_clear_password’)

@property
def width(self):
    '''获取屏幕宽度'''
    return self._driver.get_window_size().get('width', 0)

@property
def height(self):
    '''获取屏幕高度'''
    return self._driver.get_window_size().get('height', 0)

def start(self):
    if self._driver == None:
        desired_caps = {}
        desired_caps['platformName'] = 'android'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'com.qekj.merchant'
        desired_caps['appActivity'] = 'com.qekj.merchant.ui.activity.SplashActivity'
        desired_caps['platformVersion'] = '6'
        desired_caps['skipDeviceInitializatio'] = 'true'
        desired_caps['uicodeKeyBoard'] = 'true'
        desired_caps['resetKeyBoard'] = 'true'
        desired_caps['noReset'] = 'true'
        self._driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
    else:
        self._driver.launch_app()

    self._driver.implicitly_wait(10)
    return self

def login_f(self):
    #无论怎样,先点击登录账号输入框框右边"x"按钮空已填充账号
    self._driver.find_element(str(self.clear_count)).click()
    #无论怎样,先点击登录密码输入框右边"x"按钮清楚已填充账号
    self._driver.find_element(str(self.clear_psw)).click()
    #输入账号
    self._driver.find_element(str(self.account_input)).send_keys("18814810220")
    #输入密码
    self._driver.find_element(str(self.password_input)).send_keys("123456")

    try:
        self._driver.find_element(str(self.agree_button)).click()
        self._driver.find_element(str(self.login_button)).click()

    except Exception as e:
        self._driver.find_element(str(self.login_button)).click()

def stop(self):
    pass

def restart(self):
    pass

def main(self):
    '''滑动引导页,进入登录页'''
    try:
        self._driver.find_element(str(self.agree_loc)).click()
    except Exception as e:
        lg = App()
        lg.login_f()

        try:
            self._driver.find_element(str(self.update_cancel)).click()
        except Exception as e:
            pass

    return Main(self._driver)

你的某个page是不是忘记 return driver 了,报错的截图贴一下


image
这个文件也截一下吧

没有找到元素,试着使用隐式等待implicitly_wait()

单步调试看下 self._driver 的来源即可

感谢大家的支招,来迟了,后来我又试了下,将全部变量形式改成原来的id定位写法,然后重新搞了下try…catch逻辑,用例成功调试通了,之前的报错应该是元素定位问题导致的,附上修改后的代码 :blush: :handshake:
def main(self):
“”"
1、尝试点击app首页"同意更新"按钮
2、点击失败随即进行尝试找到启动页"同意进入按钮"并点击,左滑两次并进行登录。。。。。
3、第二步如果找不到元素点击失败则意味着app已登录过一次,进行下一步的判断
4、第三步失败后进行有无缓存判断登录
5、登录后进入首页判断有无更新弹窗,有则点掉
“”"
try:
sleep(2)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/ll_update’).click()
except Exception as e:
sleep(2)
try:
#进入首页滑动,点击进入
self._driver.find_element(MobileBy.ID, self.agree_loc).click()
sleep(1)
for i in range(2):
self._driver.swipe(self.width0.9,
self.height
0.5,
self.width0.1,
self.height
0.5
)
sleep(2)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/btn_open’).click()
#执行登录操作
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_account’).send_keys(“18814810220”)
sleep(1)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_password’).send_keys(“123456”)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/iv_select’).click()
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/rl_login’).click()
except Exception as e:
try:
sleep(2)
#先尝试清空登录框内容,如果清空按钮存在意味着老账号存在缓存登录,执行此情况下登录逻辑
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/iv_clear_account’).click()
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_account’).send_keys(“18814810220”)
sleep(2)
self._driver.find_element(MobileBy.XPATH, “//*[@resource-id=‘com.qekj.merchant:id/rv_phone’]/android.widget.RelativeLayout”).click()
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_password’).send_keys(“123456”)
sleep(1)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/rl_login’).click()
except Exception as e:
#新号码无缓存情况下登录
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_account’).send_keys(“18814810220”)
sleep(1)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/et_password’).send_keys(“123456”)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/iv_select’).click()
#sleep(1)
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/rl_login’).click()
#进入首页判断有无更新弹框,有则点掉
try:
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/iv_cancel’).click()
except Exception as e:
self._driver.find_element(MobileBy.ID, ‘com.qekj.merchant:id/ll_update’).click()
sleep(20)
return Main(self._driver)

用一对三个反单引号(英文输入法下的esc键下方) 就是代码的格式,这样贴上来的代码格式就不会乱

1 Like

好的,下次我注意下,感谢

关闭