用python写一个ui自动化的框架
时间: 2023-11-27 17:05:30 浏览: 79
好的,以下是一个基于Python的UI自动化框架的示例:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class UIAutomation:
def __init__(self, browser="chrome"):
if browser == "chrome":
self.driver = webdriver.Chrome()
elif browser == "firefox":
self.driver = webdriver.Firefox()
elif browser == "ie":
self.driver = webdriver.Ie()
else:
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def open_url(self, url):
self.driver.get(url)
def click_element(self, locator):
element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(locator))
element.click()
def input_text(self, locator, text):
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(locator))
element.clear()
element.send_keys(text)
def get_text(self, locator):
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(locator))
return element.text
def close(self):
self.driver.quit()
```
代码中,`UIAutomation`类封装了一些常用的UI自动化操作,包括打开网页、点击元素、输入文本、获取文本等。使用`selenium`库进行UI操作,使用`WebDriverWait`等类来实现等待页面元素加载的功能。可以根据需要选择不同的浏览器(目前支持 Chrome、Firefox、IE)。
阅读全文