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的内容主要包括属性和方法,属性不难理解,就是这个页 ...
随机推荐
- Spring MVC 以.html为后缀名访问获取数据,报406 Not Acceptable错误。
如题,最近以spring mvc作为后台框架,前端异步获取数据时(.html为后缀名的访问方式),报406 Not Acceptable错误.当初都不知道啥原因,前后台都没报错就是返回不了数据,于是查 ...
- poj_1679: The Unique MST【次小生成树】
题目链接 参考博客 希望注释足够清楚..欢迎指出不足~ #include<cstdio> #include<cstring> #include<algorithm> ...
- 【Unity游戏开发】SDK接入与集成——小白入门篇
一.简介 通常一款游戏开发到后期,一般都会涉及到第三方SDK的接入与集成,对于不熟悉SDK接入的同学来说,接SDK每次都是云里雾里,而熟悉SDK接入的同学又觉得不断地重复做接入SDK工作这样没有成就感 ...
- Mybatis jpa mini 代码解析
源码地址(git):https://github.com/LittleNewbie/mybatis-jpa 一.Mybatis简介 mybatis中文官方文档:http://www.mybatis.o ...
- 自家服务器防止DDoS攻击的简单方法
DDoS攻击并不是新技术,该攻击方式最早可以追溯到1996年,2002年时在我国就已经开始频繁出现.在DDoS攻击发展的这十几年间,DDoS攻击也在不断变化.数据显示,最大规模的DDoS攻击峰值流量超 ...
- 配置mabatis,报Could not load driverClass ${jdbc.driverClassName}
<!-- 扫描mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" ...
- linux服务器部署jar包以及shell脚本的书写
背景:记录在linux环境下部署jar程序的过程 1 部署过程记录 1.1 程序结构 这里的main函数就在DemRest2.java 文件中. 为了部署方便,要做到以下两点: 1 在导出的jar包中 ...
- 模拟EF CodeFist 实现自己的ORM
一.什么是ORM 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单来说,ORM 是通过使用描述对象 ...
- apt-get命令失效
1.linux体系区分 a.ded体系,例如debian系统.ubuntu系统.使用apt-get命令: 例如:sudo apt-get install apache2 b.rpm体系,例如redha ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...