selenium 封裝

週末無聊 在家封裝一個pyselenium。可能這些封裝你們都會使用,可是我仍是根據我本身的習慣去選擇性的去封裝一些在我工做中用的,這樣的話,我就不用去看selenium的api的,我能夠根據我本身的封裝去寫腳本,這樣的,我仍是能夠更加靈活快捷的去完成個人代碼,其實我相信不少的網上都會有大神對這個的封裝,那麼我也是根據我業餘學習的只知識,在加上我本身的摸索去封裝的幾個方法,其實我也沒怎麼去大規模的去試下這個是否可以更快的去使用,可是我封裝的函數都是我本身能看懂的,能夠直接去使用的。css

通過簡單的封裝,讓咱們在使用的時候更加方便快捷,更加用起來順手,雖然筆者未曾大規模的使用,可是也是根據個人經驗去試着去對這些進行封裝python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

class pyselenium():
	def __init__(self,brower):#初始化瀏覽器
		if brower =='firefox' or brower =='Firefox' or brower =='f' or brower =='F':
		 	deriver=webdriver.Firefox()
		elif brower =='Ie' or brower =='ie' or brower =='i' or brower=='I':
			deriver=webdriver.Ie()
		elif brower =='Chrome' or brower =='chrome' or brower =='Ch' or brower=='ch':
			deriver=webdriver.Chrome()
		elif brower =='PhantomJS' or brower =='phantomjs' or brower =='ph' or brower=='phjs':
			deriver=webdriver.PhantomJS()
		elif brower =='Edge' or brower =='edge' or brower =='Ed' or brower=='ed':
			deriver=webdriver.Edge()
		elif brower =='Opera' or brower =='opera' or brower =='op' or brower=='OP':
			deriver=webdriver.Opera()
		elif brower =='Safari' or brower =='safari' or brower =='sa' or brower=='saf':
			deriver=webdriver.Safari()
		else:
			raise NameError('只能輸入firefox,Ie,Chrome,PhantomJS,Edge,Opera,Safari')
		self.driver=deriver
	def element(self,fangfa,dingwei):#定位
		if fangfa=='id':
			element=self.deriver.find_element_by_id(dingwei)
		elif fangfa == "name":
			element = self.driver.find_element_by_name(dingwei)
		elif fangfa == "class":
			element = self.driver.find_element_by_class_name(dingwei)
		elif fangfa == "link_text":
		    element = self.driver.find_element_by_link_text(dingwei)
		elif fangfa == "xpath":
			element = self.driver.find_element_by_xpath(dingwei)
		elif fangfa == "tag":
			element = self.driver.find_element_by_tag_name(dingwei)
		elif fangfa == "css":
			element = self.driver.find_element_by_css_selector(dingwei)
		else:
			raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css','tag'.")
		return element
	def elements(self,fangfa,dingwei):#組定位
		if fangfa=='id':
			element=self.driver.find_elements_by_id(dingwei)
		elif fangfa == "name":
			element = self.driver.find_elements_by_name(dingwei)
		elif fangfa == "class":
			element = self.driver.find_elements_by_class_name(dingwei)
		elif fangfa == "link_text":
			element = self.driver.find_elements_by_link_text(dingwei)
		elif fangfa == "xpath":
			element = self.driver.find_elements_by_xpath(dingwei)
		elif fangfa == "tag":
			element = self.driver.find_elements_by_tag_name(dingwei)
		elif fangfa == "css":
			element = self.driver.find_elements_by_css_selector(dingwei)
		else:
			raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css','tag'.")
		return elements
	def element_wait(self,fangfa,dingwei,wati=6):#等待
		if fangfa == "id":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.ID, dingwei)))
		elif fangfa == "name":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.NAME, dingwei)))
		elif fangfa == "class":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.CLASS_NAME, dingwei)))
		elif fangfa == "link_text":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.LINK_TEXT, dingwei)))
		elif fangfa == "xpath":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.XPATH, dingwei)))
		elif fangfa == "css":
			WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.CSS_SELECTOR, dingwei)))
		else:
			raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css'.")
	def open(self,url):#打開網頁
		self.driver.get(url)
	def make_maxwindow(self):#最大化瀏覽器
		self.driver.maximize_window()
	def set_winsize(self,wide,hight):#設置窗口
		self.driver.set_window_size(wide,hight)
	def send_ke(self,fangfa,dingwei,text):#發送內容
		self.element(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		e1.clear()
		e1.send_keys(text)
	def clea(self,fangfa,dingwei):#清空
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		e1.clear()
	def click(self,fangfa,dingwei):#單擊
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		e1.click()
	def right_click(self,fangfa,dingwei):#右擊
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		ActionChains(self.deriver).context_click(e1).perform()
	def move_element(self,fangfa,dingwei):#移動到
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		ActionChains(self.deriver).move_element(e1).perform()
	def double_click(self,dingwei,fangfa):#雙擊
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		ActionChains(self.driver).double_click(e1).perform()
	def  drag_and_drop(self,fangfa1,e1,fangfa2,e2):#從e1到e2
		self.element_wait(fangfa1,e1)
		eme1=self.element(fangfa1,e1)
		self.element_wait(fangfa2,e2)
		eme2=self.element(fangfa2,e2)
		ActionChains(self.deriver).drag_and_drop(eme1,eme2).perform()
	def click_text(self,text):#點擊文字
		self.driver.find_element_by_link_text(text).click()
	def close(self):#關閉
		self.driver.close()
	def kill(self):#退出
		self.driver.quit()
	def sublimit(self,fangfa,dingwei):#提交
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		e1.sublimit()
	def f5(self):#刷新
		self.driver.refresh()
	def js(self,sprit):#執行js
		self.driver.execute_script(sprit)
	def get_attribute(self, fangfa,dingwei, attribute):
		e1=self.element(fangfa,dingwei)
		return e1.get_attribute(attribute)
	def get_text(self,fangfa,dingwei):
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		return e1.text
	def get_is_dis(self,fangfa,dingwei):
		self.element_wait(fangfa,dingwei)
		e1=self.element(fangfa,dingwei)
		return e1.is_displayed()
	def get_title(self,fangfa,dingwei):#獲取title
		return self.driver.title()
	def get_screen(self,file_path):#截屏
		self.driver.get_screenshot_as_file(file_path)
	def wait(self,fangfa,dingwei):#等待
		self.driver.element_wait(fangfa,dingwei)
	def accpet(self):#容許
		self.driver.switch_to.alert.accept()
	def dismiss_alert(self):
		self.driver.switch_to.alert.dismiss()
	def switch_to_frame(self, fangfa,dingwei):#切換
	 	self.element_wait(fangfa,dingwei)
	 	if1=self.element(fangfa,dingwei)
	 	self.driver._switch_to.frame(if1)
if __name__ == '__main__':
	driver=pyselenium(brower="firefox")
	driver.open('http://www.baidu.com')
相關文章
相關標籤/搜索