selenium 封装
周末无聊 在家封装一个pyselenium。可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的封装去写脚本,这样的,我还是可以更加灵活快捷的去完成我的代码,其实我相信很多的网上都会有大神对这个的封装,那么我也是根据我业余学习的只知识,在加上我自己的摸索去封装的几个方法,其实我也没怎么去大规模的去试下这个是否能够更快的去使用,但是我封装的函数都是我自己能看懂的,可以直接去使用的。
经过简单的封装,让我们在使用的时候更加方便快捷,更加用起来顺手,虽然笔者不曾大规模的使用,但是也是根据我的经验去试着去对这些进行封装
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')
selenium 封装的更多相关文章
- Selenium - 封装WebDrivers (C#)
Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...
- 【转】Selenium - 封装WebDrivers (C#)
本文转载自:http://www.cnblogs.com/qixue/p/3977135.html Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Se ...
- Selenium封装
import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...
- python+selenium封装UI自动化框架
seleinum框架 框架的思想: 解决我们测试过程中的问题:大量的重复步骤,用自动化来实现 1)配置和程序的分离 2)测试数据和程序的分离 3)不懂编程的人员可以方便使用:使用的 ...
- selenium 封装代码
package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...
- Java Selenium封装--RemoteWebDriver
package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...
- Java Selenium封装--RemoteWebElement
package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...
- selenium之封装登陆操作
# selenium 封装登录操作举例 import os, time # from selenium import webdriver class LoginPage(): '''登录模块''' d ...
- Python+Selenium框架设计之框架内封装基类和实现POM
原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN 博客地址https ...
随机推荐
- P1040 加分二叉树
转自:(http://www.cnblogs.com/geek-007/p/7197439.html) 经典例题:加分二叉树(Luogu 1040) 设一个 n 个节点的二叉树 tree 的中序遍历为 ...
- IntelliJ IDEA(三) :常用快捷键
说IDEA对新手来说难,可能其中一个原因就是快捷键组合多而且复杂但是它也很全,基本所有功能都可以通过快捷键来完成,可以这么说,如果你掌握了所有IDEA的快捷键使用,那么你完全可以丢掉鼠标,而且不影响开 ...
- nyoj 63 小猴子下落 思维
nyoj 63 小猴子下落 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=63 思路: 不需要用指针创建二叉树,也不需要用数组来模拟二叉 ...
- PHP实现excel
导入 public function excel_put(){ //先做一个文件上传,保存文件 $path=$_FILES['file']; $filePath = "uploads/&qu ...
- CSS3 box-shadow 内外阴影效果
说明 box-shadow 属性可以给元素边框周围添加一个或者多个阴影效果.定义多个阴影,使用逗号分隔. 语法 box-shadow: none | [inset? && [<o ...
- vue-cli 使用小技巧
1.关闭烦人的eslint 语法检测,在 config 文件夹下 设置: // Use Eslint Loader? // If true, your code will be linted duri ...
- 数据库 E-R模型
数据库 E-R模型被定义被两种模型 "实体模型" AND "关系模型" 1.1 实体模型 如图:这是一个"项目表" Project ...
- 调用WebService获取数据
以下调用方法,以调用苏州天气接口为例. 一.后台请求服务 方法一.C#后台,通过构建Soap请求接口数据 //获取天气详细信息 public JsonResult GetWeatherDetails( ...
- Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码
Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码 左右側滑效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a ...
- 网络协议之ipv6
1. 地址分类 比較重要的主要有以下几种: 本地链路地址:用于链路之间相互通信 本地网站地址:用于子网内互相通信,类似于ipv4中的私有地址 全球单播地址:类似于ipv4中的公网地址 组播地址 2. ...