import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException class Driver(object):
@staticmethod
def getDriver(option): if option == "gc":
driver = webdriver.Chrome()
elif option == "ff":
driver = webdriver.Firefox()
elif option == "ie":
driver = webdriver.Ie()
else:
raise NameError("目前暂时只支持三剑客浏览器,option desc ==> gc:Chrome,ff:Firefox,ie:IE")
driver.implicitly_wait(time_to_wait=20)
driver.maximize_window()
return driver class EleUtil(Driver):
driver = Driver.getDriver("gc") @staticmethod
def find_element(*loc):
return EleUtil.driver.find_element(*loc) @staticmethod
def find_elements(*loc):
return EleUtil.driver.find_element(*loc) class Base(EleUtil):
@staticmethod
def openPage(url):
return EleUtil.driver.get(url) @staticmethod
def getTitle():
return EleUtil.driver.current_url def wait_located_element(self, timeout, *loc_tuple):
# 判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement
try:
ele = WebDriverWait(EleUtil.driver, timeout).until(EC.presence_of_element_located(*loc_tuple))
if ele:
return ele except TimeoutException: raise NoSuchElementException("No such element") def wait_visibility_element(self, timeout, *loc_tuple):
try: ele = WebDriverWait(EleUtil.driver, timeout).until(EC.visibility_of_element_located(*loc_tuple))
if ele:
return ele
except TimeoutException:
raise NoSuchElementException("No such element") def waitUtilVisibilityEle(self, timeout, ele):
try:
element = WebDriverWait(EleUtil.driver, timeout).until(EC.visibility_of(ele))
if element:
return element
except Exception:
raise NoSuchElementException("no such element.....") def waitUtilVisibility_Elements(self, timeout, *loc): elements = WebDriverWait(EleUtil.driver, timeout).until(EC.presence_of_all_elements_located(*loc))
return elements def move_to(self, ele):
ActionChains(EleUtil.driver).move_to_element(to_element=ele).perform() def refresh(self):
EleUtil.driver.refresh() def screen(self, img_path):
EleUtil.driver.get_screenshot_as_file(img_path) def selectNewWindow(self):
cur_handle = EleUtil.driver.current_window_handle
for handle in EleUtil.driver.window_handles:
if handle != cur_handle:
EleUtil.driver.switch_to.window(handle)

 

2.框架引用uittest

import unittest
from lib.selenium_utils import *
from selenium import webdriver
import time
from time import sleep class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("打开浏览器")
cls.a = time.clock()
Base.openPage("http://www.baidu.com") @classmethod
def tearDownClass(cls):
Base.driver.quit()
b = time.clock() - cls.a
print("关闭浏览器", b) def test_method1(self):
Base.find_element(By.ID, "kw").send_keys("selenium2 python")
Base.find_element(By.ID, "su").click()
print(Base.getTitle()) def test_method2(self):
Base.openPage("https://www.cnblogs.com/Rita-LJ/p/8079094.html")
print(Base().getTitle()) if __name__ == "__main__":
unittest.main() # 程序入口

  

封装模式二:

class Base(object):
def __init__(self,driver): self.driver=driver def find_element(self,*loc):
return self.driver.find_element(*loc) def find_elements(self,*loc):
return self.driver.find_elements(*loc) def openPage(self,url):
self.driver.get(url) def getTitle(self):
return self.driver.current_url

框架引用

from selenium import webdriver
from lib.tett import Base
import time
from selenium.webdriver.common.by import By
import unittest class Test(unittest.TestCase): def setUp(self):
print("打开浏览器")
self.a=time.clock()
self.driver=webdriver.Chrome()
self.driver.maximize_window()
Base(self.driver).openPage("http://www.baidu.com")
print(Base(self.driver).getTitle())
def tearDown(self):
self.driver.close()
b=time.clock()-self.a
print("close浏览器",b)
def test_1(self):
Base(self.driver).openPage("http://www.vip.com")
print(Base(self.driver).getTitle())
def test_2(self):
Base(self.driver).find_element(By.ID,"kw").send_keys("selenium2 python")
Base(self.driver).find_element(By.ID,"su").click()
print(Base(self.driver).getTitle())
if __name__ == "__main__":
unittest.main()

  

Selenium封装的更多相关文章

  1. Selenium - 封装WebDrivers (C#)

    Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...

  2. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  3. 【转】Selenium - 封装WebDrivers (C#)

    本文转载自:http://www.cnblogs.com/qixue/p/3977135.html Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Se ...

  4. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  5. selenium 封装代码

    package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...

  6. Java Selenium封装--RemoteWebDriver

    package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...

  7. Java Selenium封装--RemoteWebElement

    package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...

  8. selenium之封装登陆操作

    # selenium 封装登录操作举例 import os, time # from selenium import webdriver class LoginPage(): '''登录模块''' d ...

  9. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

随机推荐

  1. 【SSH网上商城项目实战07】Struts2和Json的整合

    转自:https://blog.csdn.net/eson_15/article/details/51332758 上一节我们完成了DataGrid显示jason数据,但是没有和后台联系在一起,只是单 ...

  2. Tomcat的下载安装及使用

    macOS Sierra Version 10.13.2 环境下Tomcat的下载与安装以及InterlliJ IDEA 2017.2 环境下配置Tomcat 与创建Web项目 一.Tomcat的下载 ...

  3. JS Error 内置异常类型 处理异常 Throw语句

    Exceptional Exception Handling in JavaScript       MDN资料 Anything that can go wrong, will go wrong. ...

  4. php自动获取上一个月的起始时间

    1.借鉴评论的方法[20170309 edit] function get_month_start_end($timestamp) { !empty($timestamp) OR $timestamp ...

  5. 使用Netty3或Netty4发布Http协议服务

    现在是2018年1月11日18:12分,已经是下班时间了,小Alan今天给大家简单的介绍一下Netty,让大家以后在使用到Netty的时候能够有一定的了解和基础,这样深入学习Netty以及以后灵活应用 ...

  6. Hadoop完全分布分布式配置

    1.准备三台虚拟机.安装Ubuntu操作系统,具体过程省略 2.三台虚拟机上分别安装Java环境,具体过程省略(保证三者的Java路径一致) 3.三台机器分别配置ssh本机免密码登录 (1)安装ssh ...

  7. 在AndroidStudio中数据存储第三方数据管理Bmob的使用

    ---恢复内容开始--- 在日常写代码的过程中我们比较痛苦的就是数据库的建立和使用,那么今天来介绍一下一个第三方的数据管理平台Bmonb. 一.我们首先进入Bmob的官网创建一个账号 Bome官网网址 ...

  8. Oracle使用ODBC连接配置

    该配置是在windows 7 32位下进行的,程序已经通过了测试(使用VBS进行的测试) 1.文件下载 ------------------------------------------------ ...

  9. PL/SQL编程基础——PL/SQL简介

    课程教师:李兴华 课程学习者:阳光罗诺 日期:2018-07-28 知识点: 1. 了解PL/SQL的主要特点 2. 掌握PL/SQL块的基本结构 PL/SQL PL/SQL是Oracle在关系数据库 ...

  10. linux下安装apache与php

    http://www.92csz.com/study/linux/16.htm 1.apache 在如下页面下载apache的for Linux 的源码包 http://www.apache.org/ ...