selenium学习笔记(一)
来源:互联网 发布:javascript视频网站 编辑:程序博客网 时间:2024/06/05 16:01
最近稍微学习了一下selenium,从0开始,在这里记录一下自己的学习过程,既可以方便自己回顾,也可以方便读者更快地学习使用。内容若有不对地方,还请帮忙改正。
1. 相关简介
Selenium是一个用于Web应用程序测试的工具。
Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样。
支持的浏览器包括IE,Chrome和Firefox等。
可以借助selenium来做自动化测试,并且支持的语言包括C#,java,python等。还可以借助selenium来构建动态网页爬虫工具。
PhantomJS是一个基于webkit的JavaScript API。它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码。任何你可以在基于webkit浏览器 做的事情,它都能做到。它不仅是个隐形的浏览器,提供了诸如CSS选择器、支持Web标准、DOM操作、JSON、HTML5、Canvas、SVG等, 同时也提供了处理文件I/O的操作,从而使你可以向操作系统读写文件等。PhantomJS的用处可谓非常广泛,诸如前端无界面自动化测试(需要结合 Jasmin)、网络监测、网页截屏等。
2.安装
安装phantomjs
(1)build
sudo apt-get install build-essential g++ flex bison gperf ruby perl \
libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
libpng-dev libjpeg-dev python libx11-dev libxext-dev
(2)下载phantomjs,它可安装在windows,Linux,mac平台上,从官网http://phantomjs.org/download.html上可以下载。如果下载很慢,可以从这个链接上下载http://download.csdn.net/detail/yangyangrenren/9727380
(3)解压
tar -xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo cp -R phantomjs-2.1.1-linux-x86_64 /usr/local/share/
sudo ln -sf /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/
(4)测试是否安装成功
phantomjs –version
还可以直接在命令行界面中输入phantomjs进入到phantomjs的环境
ctrl+c,退出phantomjs环境
安装selenium
sudo pip install -U selenium
这是使用pip形式安装的,需要提前安装好pip,办法可以参考http://blog.csdn.net/yangyangrenren/article/details/53763251
3.测试
新建文件selenium_test.py
from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry: browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")except NoSuchElementException: assert 0, "can't find seleniumhq"browser.close()
python3 selenium_test.py
运行可能会报错
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
进入到https://github.com/mozilla/geckodriver/releases/ 下载对应版本的文件,然后解压,把解压后的文件放到/usr/bin/下就可以了,再次运行上述代码,当火狐浏览器成功打开yahoo界面则说明成功
还可以使用selenium来输出title
新建文件selenium_test1.py
from selenium import webdriver from pyvirtualdisplay import Display if __name__ == '__main__': display = Display(visible=0, size=(800, 800)) display.start() #browser = webdriver.Chrome() browser=webdriver.Firefox() browser.get('http://www.baidu.com') print (browser.title)
想要成功运行上述代码,需要先安装pyvirtualdisplay,命令如下
sudo pip install -U pyvirtualdisplay
可能还会报错
Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/easyprocess/__init__.py", line 225, in start env=self.env, File "/usr/lib/python3.4/subprocess.py", line 859, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child raise child_exception_type(errno_num, err_msg)FileNotFoundError: [Errno 2] No such file or directory: 'Xvfb'During handling of the above exception, another exception occurred:Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/easyprocess/__init__.py", line 178, in check_installed self.call() File "/usr/local/lib/python3.4/dist-packages/easyprocess/__init__.py", line 194, in call self.start().wait(timeout=timeout) File "/usr/local/lib/python3.4/dist-packages/easyprocess/__init__.py", line 230, in start raise EasyProcessError(self, 'start error')easyprocess.EasyProcessError: start error <EasyProcess cmd_param=['Xvfb', '-help'] cmd=['Xvfb', '-help'] oserror=[Errno 2] No such file or directory: 'Xvfb' return_code=None stdout="None" stderr="None" timeout_happened=False>During handling of the above exception, another exception occurred:Traceback (most recent call last): File "selenium_test1.py", line 5, in <module> display = Display(visible=0, size=(800, 800)) File "/usr/local/lib/python3.4/dist-packages/pyvirtualdisplay/display.py", line 34, in __init__ self._obj = self.display_class( File "/usr/local/lib/python3.4/dist-packages/pyvirtualdisplay/display.py", line 52, in display_class cls.check_installed() File "/usr/local/lib/python3.4/dist-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed ubuntu_package=PACKAGE).check_installed() File "/usr/local/lib/python3.4/dist-packages/easyprocess/__init__.py", line 180, in check_installed raise EasyProcessCheckInstalledError(self)easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']OSError=[Errno 2] No such file or directory: 'Xvfb'Program install error! You can install it in terminal:sudo apt-get install xvfb
那就执行sudo apt-get install xvfb (错误提示中有这个代码)
再次运行python3 selenium_test1.py ,可以得到网页的标题,如下
百度一下,你就知道
当然,在selenium_test1.py中,也可使用browser = webdriver.Chrome()来调用谷歌浏览器来执行,我只使用了火狐来执行,用谷歌执行具体可以参考http://www.cnblogs.com/titicia/p/6036572.html
参考网址:
- https://zhidao.baidu.com/question/147041701.html
- http://blog.csdn.net/wang1144/article/details/48651957
- http://phantomjs.org/quick-start.html
- http://www.cnblogs.com/zzhzhao/p/5380376.html
- http://www.cnblogs.com/kongzhongqijing/p/3531488.html
- http://www.cnblogs.com/titicia/p/6036572.html
- http://blog.csdn.net/hackcoder/article/details/51166292
- https://pypi.python.org/pypi/PyVirtualDisplay
- selenium学习笔记(一)
- 一、Selenium 学习笔记
- Selenium WebDriver学习笔记(一)
- selenium-webdriver (python)(一)学习笔记
- selenium+python:脚本学习笔记(一)
- Selenium-WebDriver学习笔记(一)
- Python-Selenium学习笔记一
- selenium学习---(一)
- Webdriver学习笔记(一)浅谈selenium 2.0和安装
- Python Selenium 学习笔记(一)基本操作
- Selenium学习笔记(一)——简介
- 【小白】selenium入门(一)学习笔记+登录淘宝
- selenium基础笔记(一)
- selenium 学习笔记一--selenium多窗口问题
- Selenium webdriver 学习笔记(一) - 环境搭建
- selenium 2.0 webdriver 学习笔记(一)
- python+selenium自动化测试学习笔记一
- python selenium学习(一)
- 这5种必知的大数据处理框架技术,你的项目到底应该使用其中的哪几种
- WCF身份验证之用户名密码认证
- Spring学习(五)OpenSessionInView
- 摩拜单车
- hive: command not found错误解决
- selenium学习笔记(一)
- VS2010下对话框添加菜单栏和工具栏及其启用,禁用同步
- selenium+jenkins网页自动化测试的构建
- Ajax GET请求
- jface databinding:使用CheckboxTableViewer实现表中(Set)对象与CheckTable中选中条目数据绑定
- 计算机网络(谢希仁第六版)3-24和3-25整理
- request.getSession(false)和request.getSession(true)用法区别
- linux下串口编程简单实例
- adb server version (31) doesn’t match this client (36); killing… * daemon started successfully *