Selenium封装
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封装的更多相关文章
- Selenium - 封装WebDrivers (C#)
Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...
- selenium 封装
周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...
- 【转】Selenium - 封装WebDrivers (C#)
本文转载自:http://www.cnblogs.com/qixue/p/3977135.html Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Se ...
- 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 ...
随机推荐
- [LeetCode] Three Sum题解
Three Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...
- DB2 Metadata
http://www.devart.com/dotconnect/db2/docs/MetaData.html Instead of specifying the metadata collectio ...
- bootstrap fileinput +springmvc图片上传-krajee
引入的文件 <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/filei ...
- BZOJ P4720[Noip2016]换教室____solution
题目太长不表 <--无形传送,最为致命 学习一点数学期望的基础,预处理最短路,然后加上DP即可.(废话) 理解决策和结果的差别: 在这里每阶段的决策有两个:申请|不申请 结果有两个:换|不换 然 ...
- Typescript中一些不理解的概念解释(泛型、断言、解构、枚举)
新的项目想使用typescript,因此又对其概念及使用过一遍,本文主要记录下对之前一些概念不太理解的地方. 1.泛型 定义: 在定义函数.接口或者类的时候,不预先指定具体的类型,而是在使用的时候再指 ...
- PDO drivers no value in Windows 或 ndefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
把办公室的drupal7.54版本放到自己的笔记本(OS:Windows10 Pro,php:7.0.9,mysql 5.7.11,apache:2.4)上运行不了,查看了各项配置应该没问题啊.之前还 ...
- Node.js学习(篇章一)
<node.js的特点> 采用了异步式I/O与事件驱动的架构设计,架构为单线程模型. <supervisor包的作用> node.js开发项目,当修改项目时,需要终止进程重启N ...
- C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序
基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...
- Linux / mysql: is it safe to copy mysql db files with cp command from one db to another?
Copying is very simple for MyISAM and completely 100% risky (near suicidal) with InnoDB. From your q ...
- LaTeX 使用:itemize,enumerate,description 用法
itemize和enumerate还有description 是LaTeX里列举的三种样式,分别讲一些使用技巧.itemize(意为分条目): \begin{itemize} \item[*] a \ ...