if '用户名或密码错误' in res.page_source: assert False, '登录失败,用户名或密码错误' else: assert 'Dashboard - Leon CRM' in res.title 如何将错误信息改成The password can not be less than 6 digits或者Please enter a valid email Password
时间: 2024-02-07 15:04:10 浏览: 32
您可以使用以下代码将错误信息改为"The password can not be less than 6 digits"或者"Please enter a valid email Password":
```python
if '用户名或密码错误' in res.page_source:
error_message = res.find_element_by_xpath("//div[contains(@class, 'error-message')]").text
if "密码长度至少为6位" in error_message:
assert False, 'The password can not be less than 6 digits'
elif "请输入有效的电子邮件地址或密码" in error_message:
assert False, 'Please enter a valid email Password'
else:
assert False, '登录失败,用户名或密码错误'
else:
assert 'Dashboard - Leon CRM' in res.title
```
在这段代码中,我们首先使用 `find_element_by_xpath` 方法获取错误信息。然后,我们检查错误信息中是否包含特定的文本,如果包含,则抛出相应的自定义错误信息,否则仍然抛出默认的"登录失败,用户名或密码错误"信息。
相关问题
class Test_Login_method(BasePage): def test01_login_method(self,user,passwd): self.get(LoginResources.URL_leons.value) self.send_keys(LoginResources.LOC_账号,user) self.send_keys(LoginResources.LOC_密码,passwd) self.click(LoginResources.LOC_管理员) self.click(LoginResources.LOC_登录按钮) @pytest.mark.parametrize("arg", user_info) @logs def test01_login(self,driver,arg): page = Test_Login_method(driver) res= page.test01_login_method(arg[0],arg[1]) assert 'Dashboard - Leon CRM' in res.title 报错:> assert 'Dashboard - Leon CRM' in res.title E AttributeError: 'NoneType' object has no attribute 'title',如何修改
出现 `'NoneType' object has no attribute 'title'` 的错误通常是因为 `test01_login_method` 方法没有返回任何值。您需要在 `test01_login_method` 方法中添加返回语句,以返回一个值,以便在 `test01_login` 方法中使用。例如:
```
class Test_Login_method(BasePage):
def test01_login_method(self,user,passwd):
self.get(LoginResources.URL_leons.value)
self.send_keys(LoginResources.LOC_账号,user)
self.send_keys(LoginResources.LOC_密码,passwd)
self.click(LoginResources.LOC_管理员)
self.click(LoginResources.LOC_登录按钮)
return self.driver
@pytest.mark.parametrize("arg", user_info)
@logs
def test01_login(self,driver,arg):
page = Test_Login_method(driver)
res= page.test01_login_method(arg[0],arg[1])
assert 'Dashboard - Leon CRM' in res.title
return res.title
```
这里我们在 `test01_login_method` 方法的最后添加了 `return self.driver` 语句,并在 `test01_login` 方法中使用了 `return res.title` 语句来返回 `res.title` 属性。
> assert 'Dashboard - Leon CRM' in res.title E AttributeError: 'NoneType' object has no attribute 'title'
这个错误通常意味着代码中的某个变量或属性没有被正确地赋值或定义。在这种情况下,'res' 变量的值为 None,因此它没有 'title' 属性,导致在 assert 语句中引发 AttributeError 异常。要解决这个问题,您需要检查代码中与 'res' 变量相关的部分,确保它正确地被赋值或定义,以使其具有 'title' 属性。您还可以考虑添加一些条件语句,以确保 'res' 变量不为 None,以避免出现类似的异常。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)