Base文件:
# coding:utf-8
from selenium import webdriver
from time import sleep
# 定义页面的基础类,
class BasePage(object):
# 初始化基础类
driver = webdriver.Chrome()
def __init__(self,driver,url):
self.driver = driver
self.url = url
# 启动浏览器,访问页面
def open(self):
self.driver.get(self.url)
self.driver.maximize_window()
# 定位元素
def locator_element(self,*locator):
a1 = self.driver.find_element(*locator)
return a1
# 退出浏览器驱动
def quit(self):
sleep(2)
self.driver.quit()
Search文件:
# coding:utf-8
from Test.base_page import *
from selenium.webdriver.common.by import By
from selenium import webdriver
class SearchPage(BasePage):
input_id = (By.ID,'kw')
click_id = (By.ID,'su')
def input_text(self,text):
self.locator_element(*self.input_id).send_keys(text)
def click_element(self):
self.locator_element(*self.click_id).click()
if __name__ == '__main__':
text = 'helu'
url = 'http://www.baidu.com'
driver = webdriver.Chrome()
a1 = SearchPage(driver,url)
a1.open()
a1.input_text(text)
a1.click_element()
a1.quit()
继承的父类已经有一个启动驱动的代码,在初始化前