安装 pip install selenium

web

phantomjs下载 :http://phantomjs.org/download.html

浏览器驱动下载:http://www.seleniumhq.com/download

chrome: http://chromedriver.storage.googleapis.com/index.html?path=2.22/

#!/usr/bin/env python
# encoding: utf-8
from selenium import webdriver

driver = webdriver.Chrome()
url = 'http://www.toutiao.com/news_fashion/'

driver.get(url)

print driver.title

爬取今日头条实例,使用刷新方法,来改变文章内容,暂时还不会控制鼠标滑动来实现

#!/usr/bin/env python
# encoding: utf-8
import time
from selenium import webdriver
import itertools

driver = webdriver.Chrome()
url = 'http://www.toutiao.com/news_fashion/'
driver.get(url)
print driver.get(url)

for x in range(2):
    driver.refresh()
    titles = driver.find_elements_by_class_name("title-box")
    contents = driver.find_elements_by_class_name("abstract")
    imgs = driver.find_element_by_css_selector(".feedimg")
    for title, content, img in zip(titles, contents, itertools.repeat(imgs)):
        data = {
            'title': title.text,
            'content': content.text,
            'img': img.get_attribute('src')
        }
        print data
    time.sleep(10)

driver.close()

自动登陆的例子:

# coding:utf-8

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time
#有验证码
driver = webdriver.Chrome()
url = 'http://mp.sohu.com/'
driver.get(url)

driver.find_element_by_id("userid").clear()
driver.find_element_by_id('userid').send_keys("username")
driver.find_element_by_id("pwd").clear()
driver.find_element_by_id("pwd").send_keys('password')
driver.find_element_by_id("loginbutton").click()

time.sleep(2)
driver.close()

 scrapy+selenium+phantomjs

class judge(Spider):
    name = "judge"
    start_urls = ["http://wenshu.court.gov.cn/List/List?sorttype=1&conditions=searchWord+2+AJLX++%E6%A1%88%E4%BB%B6%E7%B1%BB%E5%9E%8B:%E6%B0%91%E4%BA%8B%E6%A1%88%E4%BB%B6"]

    def init_driver(self):
        driver = webdriver.Chrome()
        return driver 

    def parse(self,response):
        driver = self.init_driver()
        driver.get(self.start_urls[0])
        sel = Selector(text=driver.page_source)
        self.logger.info(u'---------------Parsing----------------')
        print sel.xpath("//div[@class='dataItem'][1]/table/tbody/tr[1]/td/div[@class='wstitle']/a/text()").extract()
        self.logger.info(u'---------------success----------------')

  

 

  

 

selenium2使用记录的更多相关文章

  1. 在MAC下使用Robotframework+Selenium2【第二枪】如何处理Table点击指定记录

    1.通过关键字Get Matching Xpath Count获取table中的记录 2.遍历Table所有记录 3.判断记录是否符合条件,做点击操作

  2. selenium2(WebDriver)环境搭建

    1.安装jdk并配置环境变量: jdk安装jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html环境变量 ...

  3. Selenium2学习-004-WebUI自动化实战实例-002-百度登录

    此文主要通过 百度登录 功能,进行 Selenium2 的实战实例讲解.文中所附源代码于 2015-01-17 23:33 亲测通过,敬请亲们阅览.同时,您也可参考此文进行其他网站(例如 京东.易迅. ...

  4. selenium2 安装、简单使用及浏览器启动问题解决汇总

    一.selenium2 安装 1.安装jdk并配置环境变量 jdk需要1.6版本及以上的,这个从网上下载就可以,环境变量的配置前边的随笔整理过就不多说了. 2.安装Firefox,Selenium I ...

  5. selenium2.0处理case实例(二)

    本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...

  6. selenium1,selenium2,watir的比较

    接触web方面的自动化测试,会接触几个常用的工具,selenium1,selenium2,watir 有的时候总是混淆,那么他们的优缺点啥的呢,在让你给项目选自动化框架,会选择哪个??? 1,语言的支 ...

  7. 转:Selenium2.0 click()不生效的解决办法

    除了http://573301735.com/?p=5126讲的,昨天又发现一个让我1个小时生不如死的问题,就是使用两个不同的配置文件来初始化driver,findelement方法获取到的坐标居然不 ...

  8. 基于Selenium2与Python自动化测试环境搭建

    简介: selenium 是一个web的自动化测试工具,不少学习功能自动化的同学开始首选selenium ,相因为它相比QTP有诸多有点: *  免费,也不用再为破解QTP而大伤脑筋 *  小巧,对于 ...

  9. 自动化测试之 seleniumIDE,Selenium1,selenium2和testNG入门

    由于前期三个月公司的项目一直在改需求阶段,一直是手动测试,现在项目雏形以及基本页面功能都确定下来,为了不让自己陷入天天测同一功能的无限循环中,故开始自动化测试的学习之路,也为自己以后的发展铺铺路. 一 ...

随机推荐

  1. dcmtk常用命令

    dump2dcm 把普通文件转换成含有dcm头的文件,参数为源文件,目标文件 例:dump2dcm q1.txt query.dcm 表示把q1.txt文件转换为query.dcm dcmdump 阅 ...

  2. [Usaco2008 Feb]Meteor Shower流星雨[BFS]

    Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的 ...

  3. SSRS 系列 - 使用带参数的 MDX 查询实现一个分组聚合功能的报表

    SSRS 系列 - 使用带参数的 MDX 查询实现一个分组聚合功能的报表 SSRS 系列 - 使用带参数的 MDX 查询实现一个分组聚合功能的报表 2013-10-09 23:09 by BI Wor ...

  4. FlexPaper+SWFTool+操作类=在线预览PDF(转)

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

  5. python学习之路四(类和对象1)

    #encoding:utf-8 ''' Created on 2013-7-29 @author: lixingle ''' #CLass............................... ...

  6. float的深入剖析

    float的深入剖析   float是什么? float即为浮动,在html中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”.下面解释下这个定义中的几个名词的概念: 文档流 ...

  7. A2D规则引擎

    A2D规则引擎 写了个简单的规则引擎,普通情况够用了: 比如2家公司有各自的利率计算规则,如下: 在C#方面,没有写在C#的业务逻辑代码中,而是移到了外部规则文件中,如(ACompanyRatePol ...

  8. 最小堆实现优先队列:Python实现

    最小堆实现优先队列:Python实现 堆是一种数据结构,因为Heapsort而被提出.除了堆排序,“堆”这种数据结构还可以用于优先队列的实现. 堆首先是一个完全二叉树:它除了最底层之外,树的每一层的都 ...

  9. IL反编译的实用工具

    初识Ildasm.exe——IL反编译的实用工具   Ildasm.exe 概要: 一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello Wor ...

  10. 彻底抛弃PeopleEditor,SharePoint中利用Jquery Chosen创建新的人员选择器

    彻底抛弃PeopleEditor,SharePoint中利用Jquery Chosen创建新的人员选择器 基于SharePoint平台开发时,人员选择器使用频率是非常高的,但是原生的人员选择器使用太麻 ...