Python使用类
2021-06-15 11:05
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 add(a,b):
print a + b
return