场景
针对如下页面,需进行自动化测试
1.元素定位,如下红框中可看到此处的输入框元素定位,共有22个
2.如何实现循环遍历自动输入信息,并保存
代码示例
# 元素定位
add_input_ele='//form[@class="el-form"]//input[@type="text"]'
# 定位并输入操作方法
def click_input_content(self,index,content):
self.driver.find_elements_by_xpath(aapl.add_input_ele)[index].send_keys(content)
print("定位输入框并输入信息成功")
# 数据准备
input_index={
"0":"test_webui_01",
"2":"爱美剧TV",
"3":"com.piglet_androidtv",
"4":"C79BB4D1DC52A8F7BB6DF0DF667ADE6",
"5":"test运营者",
"14":"地图导航",
"19":1,
"21":"应用宝"}
def test_click_input_content_ok(self,init_loginWeb):
init_loginWeb[2].switch_menu(comm_menu[1])
init_loginWeb[2].switch_menu(comm_menu[4])
init_loginWeb[5].click_new_add()
for (key,value) in input_index.items(): # 循环遍历并输入数据
print(key+":"+value)
init_loginWeb[5].click_input_content(int(key),value) #转为int类型
assert 1
# 报错: TypeError: list indices must be integers or slices, not str
# 原因:类型错误,此处返回的下标是str,而获取元素定位的下标需为int类型