[Python爬虫] Selenium自己主动訪问Firefox和Chrome并实现搜索截图
前两篇文章介绍了安装。此篇文章算是一个简单的进阶应用吧。它是在Windows下通过Selenium+Python实现自己主动訪问Firefox和Chrome并实现搜索截图的功能。
[Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
[Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium
自己主动訪问Firefox
能够參照前文安装Selenium环境,眼下Selenium这个用于Web应用程序測试的工具支持的浏览器包含IE、Mozilla Firefox、Mozilla
Suite、Chrome等。
可是因为Firefox是默认安装路径,webdriver能够正常訪问它,而Chrome和IE须要设置driver路径。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys reload(sys)
sys.setdefaultencoding('gb18030')
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
assert "百度" in driver.title
elem = driver.find_element_by_name("wd")
elem.send_keys("Eastmount")
elem.send_keys(Keys.RETURN)
assert "谷歌" in driver.title
driver.save_screenshot('baidu.png')
driver.close()
driver.quit()
执行效果例如以下图所看到的,自己主动调用Firefox浏览器搜索,同一时候输出断言错误:
assert "谷歌" in driver.title AssertionError
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys
reload(sys)
sys.setdefaultencoding('gb18030')
因为汉语中可能会遇到错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 33
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 35
所以此处转换成gb编码,该篇不重点介绍了。
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
创建Firefoxwebdriver实例。当中Firefox最简单,其它Chrome还须要driver和配置路径。接下来通过driver.get()打开百度URL网页,webdriver会等待网页元素载入完毕之后才把控制权交回脚本。可是,假设要打开了页面在载入的过程中包括了非常多AJAX,webdriver可能无法准确推断页面何时载入完毕。
assert "百度" in driver.title
assert "谷歌" in driver.title
接下来使用断言推断文章的标题Title是否包括“百度”和“谷歌”。相应的标题是“百度一下,你就知道”,所以当中“百度”包括,而“谷歌”会出现断言报错。
同一时候提交页面并获得返回结果,为了推断结果是否成功返回也能够使用断言。
elem = driver.find_element_by_name("wd")
webdriver提供了非常多如find_element_by_*的方法来匹配要查找的元素。如利用name属性查找方法find_element_by_name来定位输入框。审查元素name=wd。
元素定位方法能够參考官网:Locating
Elements
elem.send_keys("Eastmount")
elem.send_keys(Keys.RETURN)
driver.save_screenshot('baidu.png')
driver.close()
driver.quit()
最后是调用save_screenshot进行截图。可是图片是过程中的。如何获取最后载入的图片呢?同一时候。操作完毕并关闭浏览器。当然。也能够调用quit()方法。两者的差别在于:quit()方法会退出浏览器,而close()方法仅仅是关闭页面,但假设仅仅有一个页面被打开,close()方法相同会退出浏览器。
自己主动訪问Chrome
首先下载chromedriver并置于Chrome安装文件夹。可能会遇到错误:
WebDriverException: Message: 'chromedriver' executable needs to be in PATH.參考官网解决方法:How to use chromedriver。我採用的是设置driver环境。
代码例如以下:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
driver.quit()
须要放置chromedriver例如以下路径,同一时候能够通过代码设置。
可是因为我的Chrome可能Bug一直未修复。总是打开错误。
driver = webdriver.Chrome(executable_path="G:\chromedriver.exe")
參考资料:
构建Python+Selenium2自己主动化測试环境<二>:IE、Chrome和Firefox执行
用selenium实现某微博搜索数据的抓取
RobotFramework+seleniumlibrary Web自己主动化測试 (三)
最后希望该篇基础性文章对你有所帮助吧!假设有不足之处。还请海涵~
(By:Eastmount 2015-8-20 下午4点 http://blog.csdn.net/eastmount/)
[Python爬虫] Selenium自己主动訪问Firefox和Chrome并实现搜索截图的更多相关文章
- [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图
前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能. [Pyth ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
- Python爬虫-selenium的使用(2)
使用selenium打开chrome浏览器百度进行搜索 12345678910111213141516171819202122232425 from selenium import webdriver ...
随机推荐
- 马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解(转)
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- 我要好offer之 C++大总结
0. Google C++编程规范 英文版:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 中文版:http://zh-g ...
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
- 机器人程序设计——之如何正确入门ROS | 硬创公开课(附视频/PPT)【转】
转自:http://blog.exbot.net/archives/2966 导语:本期公开课面向想入手ROS却又不知从何下手的小伙伴,为大家梳理好学习思路. ROS和Android一样是开源的,功能 ...
- 五、 java中数组
定义数组的两种方式 class myarray1 { public static void main(String[] args) { //1.如何定义一个数组 //1.1数组的声明 String[] ...
- CI调试应用程序
该分析器将在页面下方显示基准测试结果,运行过的 SQL 语句,以及 $_POST 数据.这些信息有助于开发过程中的调试和优化. 在控制器中设置以下方法以激活该分析器: $this->output ...
- 递归,回溯,DFS,BFS的理解和模板
LeetCode 里面很大一部分题目都是属于这个范围,例如Path Sum用的就是递归+DFS,Path Sum2用的是递归+DFS+回溯 这里参考了一些网上写得很不错的文章,总结一下理解与模板 递归 ...
- AC日记——Dylans loves tree hdu 5274
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HTTP状态码之200和304
HTTP状态码之200和304 HTTP状态码(HTTP Status Code)是一种表示网页服务器响应状态的三位数字编码.通过这些数字,可以简化状态的表达.状态码有几十种,其中首位数字为1-5 ...
- 东方14ACM小组 15:Challenge 11
Challenge 11 查看 提交 统计 提问 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 262144kB 描述 给一个长为N的数列,有M次操作,每次操作是 ...