分享学习web自动化的一个小框架,欢迎一起学习交流。
# 导包
from selenium import webdriver
class TestKeyWords(object):
# 初始化
def __init__(self, url, browser_typy):
self.driver = self.open_browser(browser_typy)
self.driver.get(url)
# 调用浏览器
def open_browser(self, browser_typy):
if browser_typy == 'chrome':
driver = webdriver.Chrome()
return driver
elif browser_typy == 'firefox':
driver = webdriver.Firefox()
return driver
else:
print('浏览器类型错误')
# 元素定位
def locator(self, locator_typy, value):
if locator_typy == 'xpath':
el = self.driver.find_element_by_xpath(value)
return el
elif locator_typy == 'id':
el = self.driver.find_element_by_id(value)
return el
elif locator_typy == 'name':
el = self.driver.find_element_by_name(value)
return el
# 输入
def input_text(self, locator_typr, value, text):
self.locator(locator_typr, value).send_keys(text)
# 点击
def click_element(self, locator_typr, value, ):
self.locator(locator_typr, value).click()
# 关闭浏览器,释放资源
def quit_browser(self):
self.driver.quit()
if __name__ == '__main__':
tk = TestKeyWords('http://www.baidu.com', 'chrome')
tk.input_text('id', 'kw', '派森')
tk.click_element('id', 'su')