在用户的根目录找到了
已经换种方式解决
class mytest(unittest.TestCase):
##初始化工作
def setUp(self):
pass
def tearDown(self):
self.dr.quit()
def testfun(self):
pass_1 = False
try:
self.dr.find_element_by_id('11')
print('pass')
except:
pass_1 = True
print(pass_1)
if pass_1 == True:
self.assertIsNone(None, '存在wulh发起的任务,Fail')
else:
self.assertIsNone(not None, '存在wulh发起的任务,Fail')
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(mytest)
unittest.TextTestRunner(verbosity=2).run(suite)
self.assertIsNone(self.dr.find_element_by_name(taskname_2),'存在发起的任务,Fail')
当 self.dr.find_element_by_name(taskname_2) 找不到时直接脚本报错,报错如下,找不到时不是刚好验证不存在,不应该是 pass 吗?
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
这我就不懂了。。。
直接用'WEBVIEW',不用改试下
可自行在脚本中切换
self.dr.switch_to.context('WEBVIEW') #切换至webview
self.dr.switch_to.context('NATIVE_APP') #切换成原生
可以使用装饰器:
def getImage(function):
@wraps(function)
def get_ErrImage(self,*args, **kwargs):
try:
result = function(self,*args, **kwargs)
except:
timestr = time.strftime("%Y-%m-%d_%H_%M_%S")
self.dr.get_screenshot_as_file('E:\\Appium\\Mailchat\\image\\getscreen\\%s %s.png' %
(function.__name__timestr))
else:
logging.info (" %s 脚本运行正常" %
(function.__name__)
)
return result
return get_ErrImage
使用:
class myInitiate_addOppionTest(unittest.TestCase):
def setUp(self):
public.setUp.setUp(self)
def tearDown(self):
self.dr.quit()
@getImage
def test_myInitiate_addOppion(self):
go_loginPage(self) #访问登录页
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(myInitiate_addOppionTest)
unittest.TextTestRunner(verbosity=2).run(suite)
tks,确实是因为内有 return 导致的,将 js 语句改为如下就可以了:
js = 'return document.getElementById("元素ID").value'