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 ...
随机推荐
- Python测试开发之函数
对于初学者而言,感觉函数还是不是很好理解,尤其是当写一个脚本,或者是写一个算法,认为可能for循环就已经可以解决的问题为什么还要用函数来实现呢? 今天就来说一下函数的优点,其实函数的最大优点就是可重用 ...
- 使用tail命令实时查看日志文件
[Shell] 纯文本查看 复制代码 ? 1 tail -f /日志文件 好了.就这样用.简单吧 退出ctrl+C
- php中session 入库的实现
ini_set("session.save_handler","user");//session.gc_probability = 1 分子ini_set(&q ...
- 前端页面适配的rem换算
为什么要使用rem 之前有些适配做法,是通过js动态计算viewport的缩放值(initial-scale). 例如以屏幕320像素为基准,设置1,那屏幕375像素就是375/320=1.18以此类 ...
- java 之 组合模式(大话设计模式)
代码是一门艺术,每次看完大话设计模式后都会有新的认识,有时会感叹原来还可以这样玩,相信大家都用过递归,递归的使用一般遍历文件夹等会常用到, 今天讲的设计模式类似于递归,也比较神奇,先看下类图,稍后再帮 ...
- macOS安装beego的使用bee命令出现killed:9 解决办法
最近想搞一个restful api,发现go还是不错的,拿来研究下 beego文档:https://beego.me/quickstart 安装之后,执行bee命令,报错如下: 应该是最新mac OS ...
- springCloud zuul网关服务
第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...
- Explicit Semantic Analysis (ESA)
有别于LSA (Latent Semantic Analysis), 下列文章提出一种ESA (Explicit Semantic Analysis), 并介绍怎样使用ESA来进行语义相关性和文本分类 ...
- canvas图形函数
function drawStar(cobj,x, y, radius1, radius2, num, drawType, color) {//参数:画笔,圆心X.圆心Y,半径1,半径2,形状边,实心 ...
- 深入理解计算机系统_3e 第七章家庭作业 CS:APP3e chapter 7 homework
7.6 +-----------------------------------------------------------------------+ |Symbol entry? Symbol ...