basepage里面如何警告框处理
时间: 2023-09-10 17:05:18 浏览: 46
在 BasePage 中处理警告框时,可以添加一个方法来封装警告框处理的步骤。例如:
```python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class BasePage:
def __init__(self, driver):
self.driver = driver
def accept_alert(self):
try:
# 等待警告框出现
WebDriverWait(self.driver, 10).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
alert.accept()
except:
# 如果没有警告框,什么也不做
pass
```
在这个方法中,首先使用`WebDriverWait`等待警告框出现,如果超时则抛出异常,否则获取警告框并点击确定按钮。如果没有警告框出现,则什么也不做。
在具体的页面类中,可以直接调用这个方法来处理警告框。例如:
```python
class LoginPage(BasePage):
def login(self, username, password):
# 输入用户名和密码
self.driver.find_element_by_id('username').send_keys(username)
self.driver.find_element_by_id('password').send_keys(password)
# 点击登录按钮
self.driver.find_element_by_id('login').click()
# 处理警告框
self.accept_alert()
```
在登录页面中的`login`方法中,先输入用户名和密码,然后点击登录按钮。最后,调用`accept_alert`方法来处理可能出现的警告框。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)