Python使用类
#coding:utf8
from selenium import webdriver
from time import sleep
class urlpage(object):
#创建浏览器对象
def __init__(self,brower):
driver = None
self.brower = brower
try:
if self.brower == "ie":
self._driver = webdriver.Ie()
elif self.brower == "firefox":
self._driver = webdriver.Firefox()
elif self.brower == "chrome":
self._driver = webdriver.Chrome()
if self.brower == None:
exit()
except:
print u'浏览器选择错误或没写'
# 打开浏览器
def open_url(self,url):
self._driver.get(url)
self._driver.maximize_window()
return
# 关闭浏览器
def close_url(self):
sleep(3)
self._driver.close()
return
# 用于元素定位,输入框输入,css定位
def elenemt_loc(self,loc, value):
try:
self._driver.find_element_by_css_selector(loc).clear()
self._driver.find_element_by_css_selector(loc).send_keys(value)
except:
print u'未找到%s' % loc
return
#点击元素
def click_elenemt(self,loc):
try:
self._driver.find_element_by_css_selector(loc).click()
except:
print u'未找到%s' % loc
#浏览器后退操作
def go_back(self):
try:
self._driver.back()
except:
print u'后退失败'
#浏览器前进操作
def forward(self):
try:
self._driver.forward()
except:
print u'前进失败'
#浏览器刷新
def refresh(self):
try:
self._driver.refresh()
except:
print u'刷新失败'
return
#拖动滚动条到定位的元素位置
def drag(self,loc):
try:
drags = self._driver.find_element_by_css_selector(loc)
try:
self._driver.execute_script("arguments[0].scrollIntoView();", drags)
except:
print u'滚动条拖动失败'
except:
print u'元素未找到'
return
#多表单切换
def frame_form(self,loc):
try:
frame = self._driver.find_element_by_css_selector(loc)
try:
self._driver.switch_to_frame(frame)
except:
print u'表单切换失败'
except:
print u'表单元素未找到'
#用于测试
def add(a,b):
print a + b
return
窈窕包包,君子好逑