报错代码 父类
from logs.log import log1 from framework.base_page import BasePage class LoginPage(BasePage): def __init__(self): '''定位元素''' #账号登录按钮 self.account_btn = ['class', 'select-text'] #账号 self.user_path = ['css', 'input[placeholder="输入邮箱/手机号"]'] #密码 self.pwd_path = ['css', 'input[placeholder="输入密码"]'] #登录按钮 self.login_btn_path=['class','submit-btns'] #点击账号密码方式登录 def account_login_switch(self): self.click(self.account_btn,1) #输入框输入用户名 def user_input(self,user): self.type_input(self.user_path,user) # 输入框输入密码 def pwd_input(self, pwd): self.type_input(self.pwd_path, pwd) def account_login(self): '''默认的正确账号登录功能''' username=self.config_get('username','login') password=self.config_get('password','login') try: self.type_input(self.user_path ,username) log1.info('输入账号为%s'%username) self.type_input(self.pwd_path, password) log1.info('输入密码为%s'%password) self.click(self.login_btn_path) self.show_wait(5) log1.info('登录成功') except BaseException as e: log1.error('登录失败:%s'%e) self.get_img()子类:
from logs.log import log1 from framework.base_page import BasePage from pageobject.login_page import LoginPage class CameraList(LoginPage): def __init__(self): #相机列表定位元素 self.camera_bar_path=['xpath','//div[contains(text(),"相机列表")]'] #登录 def login_in(self): try: self.account_login_switch() self.account_login() except Exception as e: log1.error(e) #点击相机列表导航 def camera_bar_click(self): self.click(self.camera_bar_path)报错信息
2020-10-22 19:47:12,029 - selenium - ERROR - 'CameraList' object has no attribute 'account_btn' 2020-10-22 19:47:22,081 - selenium - ERROR - 报错信息: Traceback (most recent call last): File "E:\Code\PythonUI自动化实战\EasySense\framework\base_page.py", line 148, in find_element element = self.driver.find_element_by_xpath(value) File "D:\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "D:\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "D:\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "D:\Python 3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(),"相机列表")]"} (Session info: chrome=86.0.4240.111) 2020-10-22 19:47:22,400 - selenium - INFO - 截图保存成功 Error Traceback (most recent call last): File "E:\Code\PythonUI自动化实战\EasySense\framework\base_page.py", line 224, in click element.click() AttributeError: 'bool' object has no attribute 'click' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Python 3.5\lib\unittest\case.py", line 58, in testPartExecutor yield File "D:\Python 3.5\lib\unittest\case.py", line 597, in run testMethod() File "E:\Code\PythonUI自动化实战\EasySense\actionoperation\camera_list_action.py", line 14, in test_cameralist_click self.camera.camera_bar_click() File "E:\Code\PythonUI自动化实战\EasySense\pageobject\camera_page.py", line 19, in camera_bar_click self.click(self.camera_bar_path) File "E:\Code\PythonUI自动化实战\EasySense\framework\base_page.py", line 227, in click display=self.isdisplayed(element) File "E:\Code\PythonUI自动化实战\EasySense\framework\base_page.py", line 104, in isdisplayed value=element.is_displayed() AttributeError: 'bool' object has no attribute 'is_displayed' ---------- environment ---------- None Ran 1 test in 18.053s FAILED (errors=1) Process finished with exit code 1修改后父类代码,主要是把定位元素放到每个方法里面,没有放到__init__方法就运行成功了
from logs.log import log1 from framework.base_page import BasePage class LoginPage(BasePage): def __init__(self): '''定位元素''' #点击账号密码方式登录 def account_login_switch(self): # 账号登录按钮 account_btn = ['class', 'select-text'] self.click(account_btn,1) #输入框输入用户名 def user_input(self,user): self.type_input(self.user_path,user) # 输入框输入密码 def pwd_input(self, pwd): self.type_input(self.pwd_path, pwd) def account_login(self): '''默认的正确账号登录功能''' # 账号 user_path = ['css', 'input[placeholder="输入邮箱/手机号"]'] # 密码 pwd_path = ['css', 'input[placeholder="输入密码"]'] # 登录按钮 login_btn_path = ['class', 'submit-btns'] username=self.config_get('username','login') password=self.config_get('password','login') try: self.type_input(user_path ,username) log1.info('输入账号为%s'%username) self.type_input(pwd_path, password) log1.info('输入密码为%s'%password) self.click(login_btn_path) self.show_wait(5) log1.info('登录成功') except BaseException as e: log1.error('登录失败:%s'%e) self.get_img()