写一个selenium的自动化框架代码
时间: 2024-06-09 19:06:28 浏览: 18
以下是一个Selenium自动化框架的示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class SeleniumFramework:
def __init__(self, browser):
if browser.lower() == 'chrome':
self.driver = webdriver.Chrome()
elif browser.lower() == 'firefox':
self.driver = webdriver.Firefox()
elif browser.lower() == 'edge':
self.driver = webdriver.Edge()
else:
raise ValueError('Unsupported browser')
def navigate(self, url):
self.driver.get(url)
def find_element(self, locator, timeout=10):
return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
def click(self, element):
element.click()
def enter_text(self, element, text):
element.clear()
element.send_keys(text)
def wait_for_element(self, locator, timeout=10):
WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
def wait_for_text(self, locator, text, timeout=10):
WebDriverWait(self.driver, timeout).until(EC.text_to_be_present_in_element(locator, text))
def close(self):
self.driver.close()
```
这个框架中有以下功能:
1. 初始化Selenium WebDriver,支持Chrome、Firefox和Edge浏览器
2. 导航到指定URL
3. 根据元素定位器查找元素
4. 点击元素
5. 在元素中输入文本
6. 等待元素出现
7. 等待元素中的文本出现
8. 关闭WebDriver
你可以使用这个框架来编写自动化测试脚本。例如,下面是一个使用这个框架的示例测试:
```python
from selenium.webdriver.common.by import By
def test_login(selenium):
framework = SeleniumFramework('chrome')
framework.navigate('https://example.com/login')
username = framework.find_element((By.ID, 'username'))
password = framework.find_element((By.ID, 'password'))
submit_button = framework.find_element((By.XPATH, '//button[text()="Login"]'))
framework.enter_text(username, 'myusername')
framework.enter_text(password, 'mypassword')
framework.click(submit_button)
framework.wait_for_text((By.XPATH, '//h1'), 'Welcome to the site')
framework.close()
```
在这个测试中,我们使用了SeleniumFramework来查找用户名、密码和提交按钮元素,输入用户名和密码,点击提交按钮,等待欢迎消息文本出现,然后关闭WebDriver。
相关推荐
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)