from selenium import webdriver标签:web,loc,self,driver,find,dr,page,def From: https://www.cnblogs.com/littleyang/p/16769712.html
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Web_Base(object):
def __init__(self):
self.driver = None
# 打开所选浏览器,给driver赋值并返回
def open_browser(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:
print('driver error')
return self.driver
# 打开网页
def get(self, url):
self.driver.get(url)
# 浏览器最大化
def windows_max(self):
self.driver.maximize_window()
# 浏览器最小化
def windows_min(self):
self.driver.minimize_window()
# 设置浏览器大小
def windows_input(self, x, y):
self.driver.set_window_size(x, y)
# 退出浏览器
def browser_quit(self):
self.driver.quit()
# 单个元素定位
def find_ele(self, *loc, time=15):
return WebDriverWait(self.driver, time).until(
lambda x: x.find_element(*loc))
# 多个元素定位
def find_eles(self, *loc, time=15):
return WebDriverWait(self.driver, time).until(
lambda x: x.find_elements(*loc))
def ele_click(self, *loc, time=15):
self.find_ele(*loc, time).click()
# 通过定位方法判断元素可见
def element_find_located(self, *loc):
return WebDriverWait(self.driver, 15).until(
EC.visibility_of(self.find_ele(*loc)))
# 通过定位元素判断元素可见
def element_located(self, *loc):
return WebDriverWait(self.driver, 15).until(
EC.visibility_of_element_located(*loc))
def send_key(self, text, *loc):
try:
self.find_ele(*loc).clear()
self.find_ele(*loc).send_keys(text)
except Exception as e:
print('无法清除文本%s' % e)
self.find_ele(*loc).send_keys(text)
#获取title
def get_title(self):
return self.driver.title
#获取窗口
def handles(self):
return self.driver.window_handles
#切换窗口
def switch_window(self, w_handle):
self.driver.switch_to.window(w_handle)
if __name__ == '__main__':
from oppo_web.old_page.home_page import Home_Page
dr = Home_Page()
dr.open_browser()
dr.windows_max()
dr.get('url')
dr.click_look(dr)
print(dr.get_title())
dr.browser_quit()