python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用
我们都知道Selenium是一个Web的自动化测试工具,可以在多平台下操作多种浏览器进行各种动作,比如运行浏览器,访问页面,点击按钮,提交表单,浏览器窗口调整,鼠标右键和拖放动作,下拉框和对话框处理等,我们抓取时选用它,主要是Selenium可以渲染页面,运行页面中的JS,以及其点击按钮,提交表单等操作。
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get("http://www.xxxxxx.com")
data = driver.title
print data
我们为什么要用phantomjs呢?
介绍
PhantomJS是一个基于webkit的JavaScript API。任何你可以在基于webkit浏览器做的事情,它都能做到。它不仅是个隐形的浏览器(没有UI界面的浏览器),提供了诸如CSS选择器、支持Web标准、DOM操作、JSON、HTML5、Canvas、SVG等,同时也提供了处理文件I/O的操作,从而使你可以向操作系统读写文件等。PhantomJS的用处可谓非常广泛,诸如前端无界面自动化测试(需要结合Jasmin)、网络监测、网页截屏等。
windows下进行安装:
pip install selenium
phantomjs使用简单的使用方式:
from selenium import webdriver
browser = webdriver.PhantomDS('D:\phantomjs.exe') #浏览器初始化;Win下需要设置phantomjs路径,linux下置空即可
url = 'http://www.xxxxxx.com' # 设置访问路径地址
browser.get(url) # 打开网页
title = browser.find_elements_by_xpath('xxxxxx') #用xpath获取元素
for t in title: # 遍历输出
print t.text #输出其中文本
print t.get_attribute(’class’)# 输出属性值
browser.qiiit() #关闭浏览器。当出现异常时记得在任务浏览器中关闭
我们进行一个简单的对比操作,首先请回顾一下selenium webdriver的操作
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https: //www.xxxxxx.com/")
dniver.find_element_by_id('xxxxxxxx').send_keys("nxxxxxx")
dniver.find_element_by_id("xxxxxxxx").click()
driver.quit()
使用phantomjs
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(xxx,xxx) #浏览器大小
driver.get ("https: //www.xxx.com/")
dniver.find_element_by_id('xxxx').send_keys("xxxx")
dniver.find_element_by_id("xxxxxx").click()
print driver.current_url
driver.quit()
通过以上两个案例大家应该可以看出相关的一个区别所在!!
编写一个简单的断言来判断phantomjs获取得到的URL是否正确的呢:
import unittest
from selenium import webdriver
class TestOne(unittest.TestCase):
def setUp(self):
self.driver = webdniver.PhantomDS()
self.driver.set_window_size(xxx, xxx)
def test_url(self):
self.driver.get("https://www.xxx.com")
self.driver.find_element_by_id('xxxxxx').send_keys("xxxx")
self.driver.find_element_by_id("xxxxx").click()
self.assentln("https://www.xxx.com", self.driver.current_url)
def tearDown(self):
self.driver.quit() if __name__ == "__main__":
unittest.main()
那么你会发现通过以上的单元测试进行断言后是完全可以通过的。
使用PhantomJS在浏览器的一个主要优点是测试通常要快得多。
import unittest
from selenium import webdriver
import time class TestThree(unittest.TestCase):
def setUp(self):
self.startTime = time.time()
def test_unl_fire(self):
time.sleep(2)
self.driver = webdniver.Firefox()
self.driver.get("https://www.xxx.com")
button = self.driver.find_element_by_id("xxx").get_attribute("xxxx")
self.assentEquals('xxxxx', button)
def test_unl_phantom(self):
time.sleep(l)
self.driver = webdniver.PhantomDS()
self.driver.get("https://www.xxx.com")
button = self.driver.find_element_by_id("xxxx").get_attribute("xxxx")
self.assentEquals('xxxxx', button)
def tearDown(self):
t = time.time() - self.startTime
print "%s: %.3f"% (self.id(), t)
self.driver.quit() if __name__== '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestThree)
unittest.TextTestRunner(verbosity=0).run(suite)
通过两个时间上的一个对比你会发现使用phantomjs速度有多快
内容拓展:
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui
import WebDriverWait
from selenium.webdriver.support
import expected_conditions as ec
import nose.tools as nose #帐户
email = 'user'
password = 'password' # phantomjs # user agent
user_agent = 'Mozilla/5.0 (Windows NT 5.1)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/29.0.1547.66 Safari/537.36' # PhantomUS的路径
pjs_path = 'xx/node_modules/phantomjs/bin/phantomjs
dcap = {"phantomjs.page.settings.userAgent":
user_agent,
'marionette' : True
} driver = webdriver.PhantomJS(executable_path=pjs_path,
desired_capabilities=dcap)
# 5秒
wait = WebDriverWait(driver, 5)
#获取html登录页面
login_page_url = 'http://xxx'
driver.get(login_page_url)
#等到页面加载
wait.until(ec.presence_of_all_elements_located)
#检查当前网址
nose.eq_('http://xxx', driver.current_url) # login # button click
show_signin = driver.find_element_by_id('xxx')
show_signin.click() # email
login_xpath = 'xxx"]' #等待对象元素
wait.until(ec.visibility_of_element_located((By.XPATH, login_xpath))) login_id_form =driver.find_element_by_xpath(login_xpath)
login_id_form.clean()
login_id_form.send_keys(email) # password
password_xpath = 'xxxx'
#等待对象元素
wait.until(ec.visibility_of_element_located((By.XPATH, password_xpath)))
# password
password_form = driver.find_element_by_xpath(passwond_xpath)
password_form.clean()
password_form.send_keys(password)
# submit
submit_xpath = 'xxxx'
dniver.find_element_by_xpath(submit_xpath).click()
# result
driver.get('http://xxx')
#等到页面加载
wait.until(ec.presence_of_all_elements_located)
#检查当前网址
nose.eq_('http://xxx', driver.current_url)
user_email = driver.find_element_by_xpath('xxx').get_attribute(
"XXX")
nose.eq_(email, user_email)
python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用的更多相关文章
- python+selenium自动化软件测试(第10章):测试驱动TDD
测试驱动开发模式,要求开发在写业务代码的时候,先写出测试代码,同时单元测试例子决定了如何来写产品的代码,并且不断的成功的执行编写的所有的单元测试例子,不断的完善单元测试例子进而完善产品代码, 这样随着 ...
- python+selenium自动化软件测试(第9章) :Logging模块
9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即 ...
- python+selenium自动化软件测试(第8章) :多线程
前戏:线程的基础 运行多个线程同时运行几个不同的程序类似,但具有以下优点:进程内共享多线程与主线程相同的数据空间,如果他们是独立的进程,可以共享信息或互相沟通更容易.线程有时称为轻量级进程,他们并不需 ...
- python+selenium自动化软件测试(第13章):selenium面试题
前言最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下 一.selenium中如何判断元素是否存在?expected_conditions模块提供了16种判断方法 ...
- python+selenium自动化软件测试(第11章):持续集成jenkins和GitHub的使用
11.1 jenkins持续集成环境 相关安装包下载链接:http://pan.baidu.com/s/1qYhmlg4 密码:dcw2赠送jenkins集成selenium环境视频链接http:// ...
- python+selenium自动化软件测试(第16章):基础实战(3)
#coding:utf-8 from time import sleep from selenium import webdriver class cloudedge_register(object) ...
- python+selenium自动化软件测试(第15章):基础实战(2)
#coding:utf-8 #for windows/py2.7 from time import sleep from selenium import webdriver browser = web ...
- python+selenium自动化软件测试(第14章):基础实战(1)
#coding=utf- from selenium import webdriven from selenium.webdriver.common.by import By from seleniu ...
- python+selenium自动化软件测试(第7章):Page Object模式
什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页 ...
随机推荐
- redis分布式锁和消息队列
最近博主在看redis的时候发现了两种redis使用方式,与之前redis作为缓存不同,利用的是redis可设置key的有效时间和redis的BRPOP命令. 分布式锁 由于目前一些编程语言,如PHP ...
- SpringMVC+Mybatis+MySQL配置Redis缓存
SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...
- 关于使用JQuery追加Option标签时使用三元运算符添加选中属性的解决办法
$(data.resultList).each(function () { var selectedFlag = ${sessionScope.userI ...
- Hibernate 实体关联关系映射----总结
在我看来,Hibernate提供这些映射关系,常用就是一对一和多对一,并且在能不用连接表的时候尽量不要用连接表.多对多会用到,如果用到了,应该首先考虑底层数据库设计是否合理. 在实际开发中,在Hi ...
- 微信小程序(一)基本知识初识别
最近微信圈里小程序很火的样子,以前小程序刚开始的时候研究了一下,多日没关注发现一些东西都淡忘了,最后决定还是记录下来的好. 毕竟好记星比不上烂笔头嘛~ 另外有想学习小程序的同学,也可以参考下,当然如果 ...
- 【CSS3】块级元素与行内元素的区别
一.行内元素与块级函数的三个区别 行内元素的特点: 和其他元素都在一行上: 高,行高及外边距和内边距部分可改变: 宽度只与内容有关: 行内元素只能容纳文本或者其他行内元素. 行内元素设置width无效 ...
- JStorm与Storm源码分析(四)--均衡调度器,EvenScheduler
EvenScheduler同DefaultScheduler一样,同样实现了IScheduler接口, 由下面代码可以看出: (ns backtype.storm.scheduler.EvenSche ...
- ABP 框架webapi设置跨域
1.在.WebApi项目中使用 NuGet Install-Package Microsoft.AspNet.WebApi.Cors 2.在xxxWebApiModule类中添加如下代码 publi ...
- 连续子序列最大和的O(NlogN)算法
对于一个数组,例如:int[] a = {4,-3,5,-2,-1,2,6,-2}找出一个连续子序列,对于任意的i和j,使得a[i]+a[i+1]+a[i+2]+.......+a[j]他的和是所有子 ...
- HTML学习笔记2
5.超链接 3种超链接: 1. 连接到其他页面 2. 锚: (是链接页面)指给超链接起一个名字,作用是连接到本页面或者其他页面的特定位置.使用name属性给超链起名字,本页要加# 3. 连接到邮件: ...