1.数据和驱动必须分开,不管是url,还是登陆输入的用户名和密码,都需要分离开。以下一个例子为把所有定位方法封装在一起,调用时候,只需要输入参数即可智能识别不同的定位方法:
一:数据脚本: 数据.py
class test_data:
prefi_id='id_'
prefi_xpath='xpath_'
login_button_id='login'
login_button_xpath='//*[@id="login"]'
二:驱动脚本-封装所有定位方法:驱动.py
class test_action:
def (self,driver,value):
prefi_element=value.split('_')[0]#分割后判断使用哪种定位方法
element=value.split('_')[1]
if 'id' in prefi_element:
driver.find_element_by_id(element)
if 'xpath' in prefi_element:
driver.find_element_by_xpath(element)
三:驱动脚本-其他封装所有定位方法:驱动2.py
class test_action:
def (self,driver,element):
try:
driver.find_element_by_id(element)
except:
try:
driver.find_element_by_xpath(element)
except:
print('请输入正确的查找元素')