请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

运行run_main(),报错'str' object has no attribute 'get_by_local'

你好老师,执行下面的run_main(),代码执行到opera_method下的on_click()函数时报错’str’ object has no attribute ‘get_by_local’,不知道如何解决
run_main.py:

# coding = utf-8
from keywords.opera_method import OperaMethod
from keywords.get_data import GetData
from util.server import Server
class RunMain:
    def run_method(self):
        appium_server =  Server()
        appium_server.main()
        self.opera_method = OperaMethod()
        self.get_data = GetData()
        lines = self.get_data.get_case_lines()
        for i in range(1,lines):
            handle_step = self.get_data.get_handle_step(i)  # 操作步骤
            element_key = self.get_data.get_handle_element(i) #操作元素key
            print(type(element_key))
            print(element_key)
            handle_value = self.get_data.get_handle_value(i) # 操作值
            expect_element = self.get_data.get_expect_element(i) # 预期结果元素
            expect_step = self.get_data.get_expect_step(i) #获取预期步骤
            excute_method = getattr(OperaMethod,handle_step)
            if handle_value != None:
                excute_method(element_key,handle_value) # input操作
            else:
                excute_method(element_key)


            if expect_step != None:
                expect_result = getattr(OperaMethod,expect_step)
                expect_result(expect_element)

if __name__ == '__main__':
    run_main = RunMain()
    run_main.run_method()

opera_method.py:

# coding = utf-8
from util.get_by_local import getByLocal
from base.base_driver import BaseDriver
import time
class OperaMethod:
    def __init__(self):
        base_driver = BaseDriver()
        self.driver = base_driver.android_driver(0)
        self.get_by_local = getByLocal(self.driver)

    def input(self, *args):
        '''
        输入值
        :param element_key: 元素(key值)
        :param value:操作值
        :return:
        '''
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], '元素没有找到'
        element.send_keys(args[1])

    def on_click(self,*args):
        '''
        元素点击
        '''
        time.sleep(3)

        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], '元素没有找到'
        time.sleep(5)
        element.click()

    def sleep_time(self, *args):
        time.sleep(args[0])

报错如下:
图片描述

case.xlsx:
图片描述
get_by_local.py:

# coding=utf-8
from util.read_init import readIni


class getByLocal:
    def __init__(self, driver):
        self.driver = driver

    def get_element(self,key,section=None):
        rI = readIni()
        value = rI.get_value(key)
        by = value.split('>')[0]
        local_by = value.split('>')[1]

        if value != None:
            if by == 'id':
                return self.driver.find_element_by_id(local_by)
            elif by == 'class_name':
                return self.driver.find_element_by_class_name(local_by)
            elif by == 'android_uiautomator':
                return self.driver.find_element_by_android_uiautomator(local_by)

            else:
                return self.driver.find_element_by_xpath(local_by)
        else:
            return None

正在回答 回答被采纳积分+3

1回答

Mushishi 2020-03-11 21:05:00

拿到得是https://img1.sycdn.imooc.com//szimg/5e68e1d7092944a106860313.jpgacount element,这个element 没有get_by local得属性,你action里面封装了acount这个方法了么》

0 回复 有任何疑惑可以回复我~
  • 提问者 慕函数9270891 #1
    老师,我只封装了get_element方法,没有封装account_element方法,account_element这个元素是app底部的账号tab元素,现在是 点击底部账号tab,进入账号页面。应该是去执行action里面的on_click()方法吧,不用再去执行action里的account方法吧,不是很懂
    回复 有任何疑惑可以回复我~ 2020-03-11 22:40:02
  • Mushishi 回复 提问者 慕函数9270891 #2
    但是你打印出来得看到是你获取到得是acoun element ,结果你action里面没这个方法 肯定报错
    回复 有任何疑惑可以回复我~ 2020-03-11 23:40:59
  • 提问者 慕函数9270891 回复 Mushishi #3
    老师找到原因了,getattr里面没有传actionMethod函数实例的对象,而是传了ActionMethod
    回复 有任何疑惑可以回复我~ 2020-03-12 16:12:02
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信