Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python+Selenium2 搭建自动化测试环境

Python+Selenium2 搭建自动化测试环境

作者头像
阳光岛主
发布于 2018-05-17 01:42:09
发布于 2018-05-17 01:42:09
1.3K00
代码可运行
举报
文章被收录于专栏:米扑专栏米扑专栏
运行总次数:0
代码可运行

米扑科技的许多项目都用到了爬虫采集网页数据,突破反爬虫、自动化测试、回归测试也要求米扑考虑构建自动化,来提高整个团队的极致工作效率。

由于忙于需求以及产品的流程规范,现在对于测试技术方面的研究也积累了很多。不过不管做什么,做好最重要!

搞自动化主要是出于团队建设考虑,一方面为了提供测试部门的工作效率,保障产品质量;另一方面,也是为了提升团队成员的测试技能,保证Team良性发展。不过不管如何,自动化是必须要搞,不然繁琐的回归测试是没有任何效率保证和质量保障的。

初步计划通过Python作为脚本语言,Selenium作为web端的测试工具,目前主要是基于web端来构建的。

米扑博客原文:Python+Selenium2 搭建自动化测试环境

Python 安装

yum -y update

yum -y install gcc gcc-g++ python python-devel python-pip

yum -y install Xvfb firefox

pip install pyvirtualdisplay

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers unzip subversion

pypi 官网: https://pypi.python.org/pypi

pip 官网: https://pypi.python.org/pypi/pip/

bs4 官网: https://pypi.python.org/pypi/beautifulsoup4/

setuptools 官网: https://pypi.python.org/pypi/setuptools/

selenium 官网:https://pypi.python.org/pypi/selenium

tornado 官网: https://pypi.python.org/pypi/tornado

tornado 官网:http://www.tornadoweb.org

tornado github: https://github.com/tornadoweb/tornado/

PyVirtualDisplay 官网: https://pypi.python.org/pypi/PyVirtualDisplay (包含 xvfb)

PyVirtualDisplay github: https://github.com/ponty/PyVirtualDisplay

firefox 老版本下载:http://ftp.mozilla.org/pub/firefox/releases/   (推荐)

本节主要记录简单搭建Python+Selenium测试环境的过程,具体如下:

基础环境:windows 7 64bit

1、构建python开发环境,版本为当前最新版本python2.7.5

在python官方网站选择下载最新windows安装包:python-2.7.5.amd64.msi,

注意这里选择64bit的。安装完之后,需要在系统的环境变量path中加入C:\Python27,然后可以在命令行。

2、SetupTools和pip工具安装

这两个工具都是属于python的第三方工具包软件,有点类似于linux下的安装包软件,不过pip比SetupTools功能更强大。

SetupTools官方解释:Download, build, install, upgrade, and uninstall Python packages -- easily!

在python的官方网站上可以找到SetupTools的下载,这里Windows只提供了32bit的下载,setuptools-0.6c11.win32-py2.7.exe,直接双击安装即可。

pip官方解释:A tool for installing and managing Python packages.

cmd进入命令行:easy_install pip 在线安装即可。

备注:此处需要注意的是,当安装SetupTools之后,就可以在python安装目录下看到Script目录,如下图所示:

这个目录生成之后,需要在系统环境变量的中加入 path:C:\Python27\Scripts,然后才可以在命令使用easy_install命令进行pip在线安装。

3、安装 Selenium

selenium 官方下载:https://pypi.python.org/pypi/selenium#downloads

selenium 最新版本:selenium-2.53.4.tar.gz

这里因为需要将Python和Selenium进行组合,当然Selenium也提供了基于python的实现,所以就需要把Selenium的包安装到python库中去,以便于python开发时进行调用。

在cmd进入命令行:pip install selenium 执行之后,将自动化搜寻最新的selenium版本下载并安装,如下图所示:

以上显示,则表明在线安装selenium成功!

4、Python + Selenium 示例

这里可以直接在python的编辑中编写如下程序,并保存hello_selenium.py

12345678

from selenium import webdriver   driver = webdriver.Firefox() driver.get(‘https://blog.mimvp.com’) assert "blog.mimvp.com 博客".decode('utf-8') in driver.title print driver.title   driver.close()

在python编辑器里面操作F5运行即可,看看是否成功调用Firefox浏览器。。。

以上一个基础的Python+Selenium的自动化环境已经搭建完成。

Selenium 多浏览器实现

构建Python+Selenium2自动化测试环境完成之后,就需要测试支持python的selenium的版本是否都支持在不同浏览器上运行,当前我们分别在三个最通用的浏览器上(IE,Chrome,FireFox)通过脚本来测试。

1) IE 浏览器

在IE浏览器上运行测试脚本,首先需要下载IEDriverServer.exe,放在IE浏览器的安装目录且同级目录下,脚本如下:

123456789101112131415161718

import osfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keys iedriver = "C:Program FilesInternet ExplorerIEDriverServer.exe"        # IE driveros.environ["webdriver.ie.driver"] = iedriver driver = webdriver.Ie(iedriver)driver.get("https://blog.mimvp.com")assert "Python" in driver.title elem = driver.find_element_by_name("q")elem.send_keys("selenium")elem.send_keys(Keys.RETURN)assert "mimvp.com" in driver.title driver.close()driver.quit()

2)Chrome 浏览器

在Chrome浏览器上运行测试脚本,首先需要下载ChromeDriver.exe,放在Chrome浏览器的安装目录且同级目录下,脚本如下:

1234567891011121314151617

import osfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keys chromedriver = "C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver = webdriver.Chrome(chromedriver) driver.get("https://blog.mimvp.com")assert "Python" in driver.titleelem = driver.find_element_by_name("q")elem.send_keys("selenium")elem.send_keys(Keys.RETURN)assert "mimvp.com" in driver.title driver.close()driver.quit()

注意官网的介绍

Chrome Driver is maintained / supported by the Chromium project iteslf.  看来如果使用 new ChromeDriver() 的话,应该要安装 Chromium 而不是 Chrome,我现在懒得折腾了,有兴趣的童鞋可以试验一下。 

3) Firefox 浏览器

在Firefox浏览器上运行测试脚本,具体如下:

12345678910111213

from selenium import webdriverfrom selenium.webdriver.common.keys import Keys driver = webdriver.Firefox()driver.get("https://blog.mimvp.com/")assert "Python" in driver.titleelem = driver.find_element_by_name("q")elem.send_keys("selenium")elem.send_keys(Keys.RETURN)assert "mimvp.com" in driver.title driver.close()driver.quit()

Selenium 虚拟浏览器静默执行(不打开窗体)

使用selenium打开网页时,FireFox浏览器会在虚拟窗体中显示,不会在当前用户窗体中打开。

应用场景:

非常适合在服务器端执行;非常人性化的不打扰当前用户工作,赞!

On *nix, you can run WebDriver in a headless (virtual) display to hide the browser. This can be done with Xvfb.

I personally use Python on Linux, and the PyVirtualDisplay module to handle Xvfb for me.

Code for running headless would look like this:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('https://www.mimvp.com')
print browser.title
browser.quit()

display.stop()

Install dependencies on Debian/Ubuntu:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ sudo apt-get install xvfb python-pip
$ sudo pip install pyvirtualdisplay

CentOS 系统上

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ yum install Xvfb python-pip      // yum install xorg-x11-server-Xvfb
$ yum --enablerepo=remi install firefox
$ pip install pyvirtualdisplay

或 AWS EC2 系统上

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ wget https://lambda-linux.io/epll-release-2015.09-1.1.ll1.noarch.rpm
$ yum -y install epll-release-2015.09-1.1.ll1.noarch.rpm
$ yum --enablerepo=epll install firefox-compat

下载firefox,解压,软链接

wget  http://download.firefox.com.cn/releases/firefox/45.0/en-US/Firefox-latest-x86_64.tar.bz2 tar jxvf Firefox-latest-x86_64.tar.bz2 mv Firefox-latest-x86_64 firefox ln -s  /home/ec2-user/tool-server/firefox/firefox /usr/bin/firefox

参考: Announcing Firefox Browser Support for Amazon Linux

firefox 更多老版本下载:http://ftp.mozilla.org/pub/firefox/releases/  (推荐)

错误1:

如果报错,则更新selenium到最新版,错误信息:

Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.

 pip install -U selenium  (高版本CentOS 7、Firefox、Selenium,此方法没用)

错误2:

easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']

OSError=[Errno 2] No such file or directory

Program install error! 

解决:

1

pip install xvfbwrapper

错误3:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

这是因为Firefox高版本需要使用 geckodriver 来驱动,不再使用Seleniu默认自带的Firefox webdriver。

我们只需要在 github 下载 geckodriver 并将其所在的路径设为环境变量即可解决。

解决:

1

brew install geckodriver

或下载安装低版本firefox

firefox-45.0.2.tar.bz2

firefox 更多老版本下载:http://ftp.mozilla.org/pub/firefox/releases/  (推荐)

应用实例:

1234567891011121314151617181920212223242526272829303132

import bs4from selenium import webdriverfrom pyvirtualdisplay import Display  def spider_url(self, url, index, total):    print("%d/%d  -  url: %s" % (index, total, url))     content = ''    browser = None    table_soup = []    try:        display = Display(visible=0, size=(800, 600))        display.start()         # now Firefox will run in a virtual display.         # you will not see the browser.        browser = webdriver.Firefox()       # 打开 FireFox 浏览器        browser.set_page_load_timeout(60)        browser.get(url)                      content = browser.find_element_by_class_name('table')       # 通过标记id 获取网页的内容        content = browser.page_source                     self.kill_firefox(browser)        display.stop()                 content = bs4.BeautifulSoup(content, from_encoding='GB18030')        table_soup = content.find('table', {"class":"table"}).find_all("tr")    except Exception as ex:        print("error msg: " + str(ex))        self.kill_firefox(browser)

还有一种静默执行的方法(供参考):

I easily managed to hide the browser window.

Just install PhantomJS. Then, change this line:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
driver = webdriver.Firefox()

to:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
driver = webdriver.PhantomJS()

The rest of your code won't need to be changed and no browser will open. For debugging purposes, use driver.save_screenshot('screen.png') at different steps of your code.

总结

通过以上三个不同浏览器上的测试,说明selenium在python中的运用于其Java版本都是一样。

由于Firefox是默认安装路径,webdriver可以正常访问找到他,如果非系统默认安装路径,则需要跟IE和Chrome一样来设置driver路径。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年04月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
selenium + python自动化测试环境搭建
-------------------------------------------------------------
流柯
2018/08/30
8340
web自动化爬虫selenium ce
wget https://dl.google.com/linux/d... --no-check-certificate
py3study
2020/01/02
5550
linux无界面(headless)使用selenium抓取数据
老高最近遇到一个需求,linux\centos下,使用selenium技术抓取数据。本来很简单的问题,但是由于内存限制,安装X window不现实,所以一个BT的想法诞生了,是否可以在centos命令行界面运行一个虚拟的桌面,然后使用selenium控制Firefox浏览器完成一些操作,Firefox运行在虚拟的桌面中,一切操作都在命令行中完成。
老高的技术博客
2022/12/27
2K0
【老金知道】python学习(三)用python模拟登陆ZABBIX(GRAFANA)的几种方式
python学习(三)用python模拟登陆ZABBIX(GRAFANA)的几种方式
Zabbix
2021/02/03
1.2K0
[Python从零到壹] 九.网络爬虫之Selenium基础技术万字详解(定位元素、常用方法、鼠标操作)
Selenium是一款用于测试Web应用程序的经典工具,它直接运行在浏览器中,仿佛真正的用户在操作浏览器一样,主要用于网站自动化测试、网站模拟登陆、自动操作键盘和鼠标、测试浏览器兼容性、测试网站功能等,同时也可以用来制作简易的网络爬虫。
Eastmount
2021/12/02
5.4K0
[Python从零到壹] 九.网络爬虫之Selenium基础技术万字详解(定位元素、常用方法、鼠标操作)
Ubuntu安装运行无头Selenium Chrome
国内可以访问下面的地址下载chrome点开一个驱动版本有个notes.txt文件 里面有浏览器和驱动的版本对应
小锋学长生活大爆炸
2022/03/29
3.7K2
Ubuntu安装运行无头Selenium Chrome
python自动化环境搭建
selenium是测试web应用程序的框架,selenium为没有测试脚本的人提供了(seleniumide)提供了录制/回放的工具,同时它也提供了特定域的语言来编写测试脚本,如c#,java,python等,selenium可以针对最现代的web浏览器运行测试,如firefox,chrome,ie,open等浏览器,selenium可以在windows,linux等平台上部署,selenium同时是开放源码的软件,是在apache2.0许可证下发布。
无涯WuYa
2018/10/25
2.3K0
火狐谷歌模拟一个虚拟界面
在Python中进行浏览器测试时,一般我们会选择selenium这样的库来简化我们工作量。而有些时候,为了对一些动态数据进行抓取,我们会选择
周小董
2019/03/25
1K0
火狐谷歌模拟一个虚拟界面
ubuntu 16.04 (桌面与服务器版)配置Selenium+Chrome+Python3实现自动化测试
如果上面运行 sudo dpkg -i google-chrome*.deb命令之后报错
十四君
2019/11/23
1.9K0
xss bot从入门到弃坑
xss在近几年的ctf形式中,越来越受到了人们的重视,但是出xss的题目最重要的可能就是xss bot的问题了,一个合格的xss bot要稳定还能避免搅屎。
LoRexxar
2023/02/21
1.1K0
xss bot从入门到弃坑
Centos 7.6 安装seleniu
[root@penguin selenium]# firefox -version Mozilla Firefox 66.0.3
py3study
2020/01/16
9750
XSS Bot从入门到完成
xss在近几年的ctf形式中,越来越受到了人们的重视,但是出xss的题目最重要的可能就是xss bot的问题了,一个合格的xss bot要稳定还能避免搅屎。下面我们就来看看一个xss bot是怎么完成的。 bot之前 一般来说,对于xss bot来说,最重要的是要bot能够执行js,事情的本质是我们需要一个浏览器内核来解析js,这里我们一般会用selenium+webdriver。 而webdriver一般有3种chrome webdriver、firefox webdriver、phantomjs。 se
FB客服
2018/02/24
2.1K0
XSS Bot从入门到完成
Selenium常见元素定位方法和操作的学习介绍
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
菲宇
2019/10/22
2.4K0
Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
米扑代理,全球领导的代理品牌,专注代理行业近十年,提供开放、私密、独享代理,并可免费试用
阳光岛主
2019/02/18
2K0
centos7无GUI情况安装Xvfb、selenium、chrome
最近需要用到selenium浏览器抓取,在windows下对照chrome浏览器开发的代码,在linux服务器上换成phantomjs驱动后,却不能运行了,通过截图发现phantomjs渲染效果和chrome不同。于是考虑在centos上安装chrome浏览器。
拓荒者
2019/03/11
3.5K0
selenium自动登录挂stackoverflow的金牌
最近玩起stackoverflow了,其中有一项成就是  Visit the site each day for 100 consecutive days. (Days are counted in UTC.)
十四君
2019/11/28
1K0
手把手带你做UI自动化测试
互联网产品的迭代速度远高于传统软件,尤其是移动APP不但更新频繁,还需要在不同硬件、系统版本的环境下进行大量兼容测试,这就给传统测试方法和测试工具带来了巨大挑战。为满足产品敏捷开发、快速迭代的需求,自动化测试逐渐流行起来。自动化测试是把以人为驱动的测试行为转化为机器执行的一种过程。
程序员鑫港
2021/12/27
5K0
腾讯云上Selenium用法示例
崔庆才
2017/04/06
4K0
腾讯云上Selenium用法示例
Selenium-01-测试环境搭建使用
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好的工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
wencheng
2020/09/28
8641
Python Selenium库的使用「建议收藏」
Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome、Firefox、Safari等主流界面浏览器,同时也支持phantomJS无界面浏览器。
全栈程序员站长
2022/07/29
4.6K0
Python Selenium库的使用「建议收藏」
推荐阅读
相关推荐
selenium + python自动化测试环境搭建
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验