报错AttributeError: 'str' object has no attribute 'send_keys'**

来源:2-4 封装定位信息

慕数据2282311

2023-04-14

请老师帮忙看看
执行到 getlocal.get_element(‘username’).send_keys(‘13760276826’)时,报错:
File “D:/work/Auto/appiumScript/MicClientUIauto/moock/case/start_appium.py”, line 144, in NotFirst_login_Xpath
getlocal.get_element(‘username’).send_keys(‘13760276826’)
AttributeError: ‘str’ object has no attribute 'send_keys’

def startappium():
    '''
    启动appium
    :return:
    '''
    device=Devices.Check_adb_devices()
    capability = {
        "platformName": "Android",
        "deviceName": f"{device}",
        # "app": "D:\\learn\\muke_app\\imooc_8.3.3_10102001_android.apk",
        "appPackage":"cn.com.open.mooc",
        "appActivity":"com.imooc.component.imoocmain.splash.MCSplashActivity",
        "noReset":True
    }
    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capability)
    return driver
def NotFirst_login_Xpath():
    '''
    非首次登录
    Returns:
    '''
    getlocal=GetByLocals(driver)
    getlocal.get_element('mine').click()
    getlocal.get_element('clicklogin').click()
    getlocal.get_element('passwordlogin').click()
    getlocal.get_element('clearLoginData').click()
    getlocal.get_element('username').send_keys('13760276826')
from moock.util.readini import ReadIni
from appium.webdriver.common.mobileby import MobileBy
class GetByLocals:
    def __init__(self,driver):
        self.driver=driver
    def get_element(self,key):
        readinit=ReadIni()
        local=readinit.get_value(key)
        if local!=None:
            by=local.split('>')[0]
            byvalue=local.split('>')[1]
            if by=='id':
                return self.driver.find_element(MobileBy.ID,byvalue)
            elif by=='xpath':
                return self.driver.find_element(MobileBy.XPATH,byvalue)
            elif by=='ACCESSIBILITY_ID':
                return self.driver.find_element(MobileBy.ACCESSIBILITY_ID,byvalue)
            else:
                return self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,byvalue)
        else:
            return '没有该元素'
写回答

1回答

Mushishi

2023-04-14

你没找到元素,返回了str,然后后面的case就拿这个str去send key 肯定不行
0
1
慕数据2282311
谢谢老师!通过打印日志看,还真的是传错了,少写了一个字母,导致它找不到元素,所以就报错了
2023-04-14
共1条回复

Python主讲移动端自动化测试框架Appium

APP自动化基础知识、po模型、关键字模型、服务自动化、持续集成

1508 学习 · 1333 问题

查看课程