AttributeError: ‘str‘ object has no attribute ‘open_url‘


# 导入openpyxl模块
import openpyxl

from selenium20220222.Key_Package.Key import Key

 # 1、进入工作簿
excel_e = openpyxl.load_workbook('../Excel_Data/data1.xlsx')
# 2、获取工作表sheet,以list列表返回
sheet_list = excel_e.sheetnames
# 3、进入工作表
# 循环输出工作簿中的工作表
for sheet in sheet_list:
    print('*********工作表是:{0}************'.format(sheet))
    # 进入指定工作表
    sheet_res = excel_e[sheet]
    # print(sheet_res,type(sheet_res))
    # 读取表中行和列
    row_s = sheet_res.max_row
    col_s = sheet_res.max_column
    # print(row_s,col_s)
    # 读取表中的单元格值
    # cell=sheet_res.values#结果是一个迭代器
    for cell_value in sheet_res.values:  # 以行的形式循环读取工作表中的值
        # print(cell_value)
        # 过滤掉不是测试用例的数据
        if type(cell_value[0]) is int:
            # print(cell_value)
            # 想要去掉参数为None的数据
            # 定义一个字典,用于存储要传入的数据
            dict = {}  # 定义一个空字典
            dict["attr_name"] = cell_value[2]  # 定义单元格的键值,并将数据添加到字典中
            dict["attr_value"] = cell_value[3]
            dict["txt"] = cell_value[4]
            # print(dict)
            # print(dict.keys())#获取字典中的键key
            for key in list(dict.keys()):  # 判断key值是否存在dict.keys这个列表中
                # print(dict)
                if dict[key] is None:  # 判断对应这个key的值是否为None
                    del dict[key]  # 输出这个key的值为空的数据
            print(dict)

            # 执行案例
            if cell_value[1] =='browser':
                # 实例化对象,**
                #print(cell_value[1])
                key = Key(**dict)#相当于是key=Key('Chrome')
            else:
                print(cell_value[1])
                getattr(key, cell_value[1])(**dict)
 

在执行到getattr(key,cell_calue[1](**dict))时报错:AttributeError: 'str' object has no attribute 'open_url'

解决方法:

        将 关键字驱动Key这个类的实例化对象名字换一个就不报错了。可能是不能准确定位到自定义封装中的方法

            # 执行案例
            if cell_value[1] =='browser':
                # 实例化对象,**
                #print(cell_value[1])
                key_k = Key(**dict)#相当于是key=Key('Chrome')
            else:
                print(cell_value[1])
                getattr(key_k, cell_value[1])(**dict)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值