前两篇文章介绍了安装。此篇文章算是一个简单的进阶应用吧。它是在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

        首先导入Selenium.webdriver模板,它提供了webdriver的实现方法。眼下支持这些方法的有Firefox、Chrome、IE和Remote。同一时候导入Keys类,它提供了操作键盘的快捷键,如RETURE、F1、ALT等。最后导入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)

        send_keys方法能够用来模拟键盘操作。相当于是在搜索框中输入“Eastmount”再按回车键搜索。但首先要从selenium.webdriver.common.keys导入Keys类。

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玩转selenium:2-入门实例及分析 - Reiki

        构建Python+Selenium2自己主动化測试环境<二>:IE、Chrome和Firefox执行

        用selenium实现某微博搜索数据的抓取

        RobotFramework+seleniumlibrary Web自己主动化測试 (三)



        最后希望该篇基础性文章对你有所帮助吧!假设有不足之处。还请海涵~

      (By:Eastmount 2015-8-20 下午4点   http://blog.csdn.net/eastmount/



[Python爬虫] Selenium自己主动訪问Firefox和Chrome并实现搜索截图的更多相关文章

  1. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  2. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  3. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

  4. [python爬虫] Selenium常见元素定位方法和操作的学习介绍

    这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...

  5. [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒

    前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...

  6. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  7. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

  8. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  9. Python爬虫-selenium的使用(2)

    使用selenium打开chrome浏览器百度进行搜索 12345678910111213141516171819202122232425 from selenium import webdriver ...

随机推荐

  1. d3 使用随机数据生成条形图

    ).map(function(){ ,)(),); }) // 返回 [27.2, 12.9, 12.2, 6.8, 9.4, 7.1, 17.5, 30, 16.6, 24.3, 19, 16.6, ...

  2. C#中DataTable中Rows.Add 和 ImportRow 对比

    最近参加项目中,数据操作基本都是用DataTable的操作,老代码中有些地方用到DataTable.Rows.Add又有些代码用的DataTable.ImportRow,于是就对比了一下 VS查询说明 ...

  3. CodeforcesD. Aztec Catacombs

    $n \leq 300000$的完全无向图,每条边有可行和不可行的状态,一开始只有$m \leq 300000$条边是可行的,给出.每次从$x$走到$y$时,所有与$x$相连的边的可行/不可行状态会改 ...

  4. 【MFC】VS2013 动态创建快捷菜单(右键菜单)

    参考 http://blog.csdn.net/csdnzhwk/article/details/47395639 参考 http://blog.csdn.net/jiadabin/article/d ...

  5. UML学习倒腾记

    先看到http://www.jianshu.com/p/1256e2643923这篇博客,号称21分钟入门uml,也许是我太笨了吧,一下午也没有完全搞定: 使用过atom编辑器,没有完全运行出来结果. ...

  6. 作列表排列时div的table属性应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 济南day2

    我好菜啊,绝望啊orzzzzzzz 上午: 上午题解报告 下午 预计100+100+30 实际100+90+0 T2不是我的错,评测机炸了,第一个点无法运行,本机是可以过得 T1 乱搞 T2 前缀和+ ...

  8. PAT甲级练习题1001、1002

    1001 A+B Format (20 分)   Calculate a+b and output the sum in standard format -- that is, the digits ...

  9. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

  10. 为了安全,linux下如何使用某个用户启动某个进程?

    安全里有个原则,叫最小权限原则 根据这个原则,对于启动某个应用或者进程,应该赋予其最小权限,根据应用权限要求,创建一个相应权限的用户,赋予其应用相应的权限,然后使用这个用户启用这个应用 如何使用某个用 ...