[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
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并实现搜索截图的功能. [Python爬虫] ...
- [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爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
- Python爬虫-selenium的使用(2)
使用selenium打开chrome浏览器百度进行搜索 12345678910111213141516171819202122232425 from selenium import webdriver ...
随机推荐
- ORACLE object_id和data_object_id
object_id和data_object_id 都是对象的唯一标识. object_id是对象的逻辑标识 data_object_id是对象的物理标识 对于没有物理存储的对象,data_object ...
- RedHat下安装OPENCV
1.解压 unzip opencv-2.4.9.zip 2.进入目录,cmake CMakeLists.txt 生成build文件 3.使用命令 make 编译 4.使用命令 make instal ...
- count distinct 多个字段 或者 count(*) 统计group by 结果
SELECT COUNT(*) FROM( SELECT 列名 FROM 表名 where ( 条件 )GROUP BY 多字段)临时表名 例如: SELECT COUNT(*) FROM(SELEC ...
- java多线程学习-同步之线程通信
这个示例是网上烂大街的,子线程循环100次,主线程循环50次,但是我试了很多次,而且从网上找了很多示例,其实多运行几次,看输出结果并不正确.不知道是我转牛角尖了,还是怎么了.也没有大神问,好痛苦.现在 ...
- Java 基础知识总结 (四、String)
四.String public final class String extends Object implements Serializable, Comparable<String>, ...
- weblogic myeclipse小知识
新建域 http://jingyan.baidu.com/article/f7ff0bfc72904e2e27bb136f.html svn 上down下来一些新项目的时候没法添加到weblogic ...
- HTML中doctype以及target论述
首先,为什么要在每个html文档开头写入<!doctype......>呢. ...
- Cannot fetch index base URL https://pypi.python.org/simple/
这个就是相源的问题,正常安装你的根目录下会有这个pip.log文件,如下 root@liu:~# ll .pip/ total 16 drwxr-xr-x 2 root root 4096 Sep 1 ...
- Oracle 建表,递增序列,触发器,分析函数row_number() ,partition by 子句。
create table SC ( Id INTEGER, Name nvarchar2(20) , KC_Name nvarchar2(20), KC_score INTEGER , constra ...
- js 判断字符为空
function checkIsNull(value){ if(typeof value=='undefined'){ return true; } if(value==null){ return t ...