ruby环境下selenium/webdriver可以通过selenium-webdriver.gem包进行安装

gem install selenium-webdriver
 
支持语言及版本有ruby 1.8.7~1.9.2,jrbuy和rubinius
 
selenium-webdriver包含了selenium-client,在阅读的时候,要注意它们两个命名空间是在两不同的API里:
1.Selenium::WebDriver - WebDrver API
2.Selenium::Client - Selenium RC API
 
WebDrver API是继承自Selenium RC API,所以没有必要在Selenium RC API花大量的时间,我们可以直接从Selenium::WebDriver开始,并围绕两个大类:Seleniu::WebDriver:Driver和Selenium::WebDriver::Element,这是整个WebDriver API的入口。
 
API 例子:
一个简单的例子:
require "selenium-webdriver"
 
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://www.google.com.hk"
 
element = driver.find_element(:name,'q')
element.send_keys "Hello WebDriver"
element.submit
 
puts driver.title
 
driver.quit
 
Driver 例子:
# 应用javascript
puts driver.execute_script("return window.location.pathname")
 
# 利用ruby和js获取元素
element = driver.execut_script("return document.body")
driver.execut_script("return arguments[0].tagname", element) #=> "BODY"
 
# 等待一些特殊元素的出现
wait = Selenium::WebDriver::Wait.new(:timeout=>10) # seconds
wait.until{driver.find_element(:id,"foo")}
# 注:wait在new时,可以设置三个值,分别为:timeout(默认5秒),:message(默认nil),:interval(默认0.5)
 
# 选择frame
driver.switch_to.frame "some-frame" # name或id
driver.switch_to.frame driver.find_element(:id, 'some-frame') # frame
# 注:switch_to方法不仅可以选择frame,还可以处理window,alert,comfirmation等窗口
 
# 选择回主窗口
driver.swith_to.default_content
 
Element 例子:
# 获取元素属性
class_name = element.attr
 
ibute("class")
# 判断元素是否显示
element.displayed?
 
# 获取元素在页面上的相对坐标位置
element.location
element.location.x
element.location.y
 
# 将元素滚动到视频可以显示的位置,再返回元素的相对坐标
element.location_once_scrolled_into_view
 
# 获取元素的宽和高
element.size
 
# 在元素里输入空,参看Selenium::WebDriver::Keys输入值
element.send_keys :space
element.send_keys "tet", :arrow_left, "s" #=> "test", 先输入 tet, 再输入一次左方向键,再输入s
element.send_keys [:control, 'a'], "1" #=> "1", 先输入crtl+a,再输入1
 
 
# 获取元素文本
element.text
 
 
更高级的用法(见 ActionBuilder)
driver.action.key_down(:shift).click(element).double_click(second_element).key_up(:shift).drag_and_drop(element,third_element).perform
 
 
启动chrome浏览器的方法 
1.下载ChromeDriver并运行,如图
 
2.启动chrome
driver = Selenium::WebDriver.for :remote, :url=>"http://localhost:9515"
driver.get "http://www.google.com.hk"
其它操作一样。
 
如果这样感觉比较麻烦,可以将下载的chromedriver.exe路径加载到环境变量中,就可直接用了
driver = Selenium::WebDriver.for :chrome
 
 
chrome个性化启动
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "d:/download"
 
driver = Selenium::WebDriver.for :chrome, :profile=>profile
 
Remote应用
RomteWebDriver可以控制浏览器在不同的机器上运行,下载selenium-server-standlone-x.xx.x.jar
java -jar selenium-server-standalone.jar '启动服务
 
driver = Selenium::WebDriver.for :remote
 
默认情况下,可以启动服务运行在localhost:4444,并打开firefox。如果想连接其它机器上的服务,可以用:url选项
driver = Selenium::WebDriver.for :remote, :url=>"http://remoteserver:44444/wd/hub"
 
本机不用加/wd/hub,但远程一定要加
 
启动其它的浏览器,用:desired_capabilities选项
driver = Selenium::WebDriver.for :remote, :desired_capabilities=>:chrome
 
 
Selenium::WebDriver::Remote::Capabilities的例子
 
require "selenium-webdriver"
include Selenium
 
caps = WebDriver::Remote::Capabilities.htmlunit :javascript_enabled=>true
driver = WebDriver.for :remote, :desired_capalibities=>caps
 
 
 
修改remote服务的端口号,目前只支持firefox
 
include Selenium
 
caps = WebDriver::Remote::Capabilities.firefox (:proxy=>WebDriver::Proxy.new(:http=>"myproxyaddress:8888"))
driver = WebDriver.for :remote, :desired_capalibities=>caps
 
如果是一个远程的remote服务
 
include Selenium
 
client = WebDriver::Remote::Http::Default.new
 
client.proxy = Proxy.new :http=>"proxy.org:8888"
driver = WebDriver.for :remote, :http_client=>client
 
 
Firefox
加扩展插件
在用firefox时,经常要用到firebug进行查看,启动firefox带firebug
include Selenium
 
profile = WebDriver::Firefox::Profile.new
profile.add_extension "path/firebug.xpi"
 
driver = WebDriver.for :firefox, :profile=>profile
 
使用已经存在的profile
使用一个已经存在的profile模板,可以用firefox -profilemanger把profile保存出来(这个命令在ff8上似乎无用,还要进一步验证)
 
driver = Selenium::WebDriver.for :firefox, :profile=>"my_existing_profile"
 
如果想用默认profile,可以通过 :profile=>"default"
 
或者也可以这通过profile实例来使用已经存在的和自定义的profile。此方法不能修改已经存在的profile,且只能在webdriver下使用
 
default_profile = Selenium::WebDriver::Firdfox::Profile.from_name "default"
default_profile.native_events = true
 
driver = Selenium::WebDriver.for :firefox, :profile=>default_profile
 
导出firefox profile见《Firefox 8导出profile文件》
 
Firefox个性化设置
端口号
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = 'd:\download'
profile['browser.download.folderlist'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
 
driver = Selenium::WebDriver.for :firefox, :profile=>profile
 
用remote driver启动时,任然可以对firefox进行个性化设置
 
profile = Selenium::WebDriver::Firfox::Profile.new
profile['foo.bar'] = true
 
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox :firefox_profile=>profile
driver = Selenium::WebDriver.fox :remote, :desired_capabilities=>capabilities
 
设置Firefox路径
当firfox不是安装在默认路径中时,要设定firefox的安装路径
Selenium::WebDriver::Firefox.path = "/path/to/firefox" # firefox安装路径
driver = Selenium::WebDriver.for :firefox
 
 
本地事件
本地事件在window下默认是激活的,可以设置关闭:
profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = false # 关闭
 
driver = Selenium::WebDriver.for :firefox, :profile=>profile
 
理论上linux支持本地事件,profile.native_events = true # 开启
 
 
Opera
opera也是用remote的方法进行启动的。在开始之前,要下载selenium-server-standalone-x.xx.x.jar,并且新建环境变量SELENIUM_SERVER_JAR到本地系统中。
window: SELENIUM_SERVER_JAR=..\server-standalone.jar 
liunx: export SELENIUM_SERVER_JAR=path_to/server-standalone.jar 
 
便可以轻松的用Selenium::WebDriver启动Opera
 
driver = Selenium::WebDriver.for :opera
driver.navigate.to "http://www.baidu.com"
 
Timeouts
 
隐式等待
WebDriver可以设置隐式等待,所以在应用#find_element定位元素时,会等待元素的出现,直至NoSuchElementError的出现。
 
driver = Selenium::WebDriver.for :firefox
driver.manager.timeouts.implicit_wait = 3 # 等待3秒
 
显示等待
可以用wait类去实例化一些等待条件
 
wait = Selenium::WebDriver::Wait.new(:timeout=>3)
wait.until {driver.find_elenium(:id=>"foo").displayed?}
 
 
内部的超时
WebDriver使用了大量的HTTP的驱动(jsonWireProtocol)。Net::HTTP作为ruby的标准库使用,默认超时为60秒。如果用WebDriver#get启动一个加载时间超过60秒的页面,你会看Net::HTTP的TimeoutError错误,可以在启动浏览器之前修改timeout来改变默认超时时长。
 
client = Selenium::WebDriver::Remote::Http::Default.new
client.titmeout = 120 # 设置为120秒
driver = Selenium::WebDriver.for :temote, :http_client=>client
 
js弹出框
获取js的alert,prompt和comfirm弹出窗都是用switch_to
 
 
require "selenium-webdriver"
 
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"
 
driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end
 
 
用Curb或者自己的http client
HTTP通信内部默认使用Net::HTTP,如果有安装Curb gem就可以选择这么做
 
require "selenium/webdriver/remot/http/curb"
include Selenium
 
client = WebDriver::Remote::Http::Curb.new
driver = WebDriver.for :remote, :http_client=>client

selenium-webdriver 简单教程的更多相关文章

  1. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

  2. 【转载】Selenium WebDriver的简单操作说明

    转载自:http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏 ...

  3. Selenium WebDriver的简单操作说明

    [From] http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才 ...

  4. (java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出

    selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出: 该情况适合能能循环page=1~n,并且每个网页随着循环可以打开的情况, 注意一定是自己拼接的url可以 ...

  5. selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)

    selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver   selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...

  6. Selenium WebDriver TestNg Maven Eclipse java 简单实例

    环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...

  7. Selenium WebDriver java 简单实例

    开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...

  8. jmeter联合selenium webdriver进行自动化测试-简单1

    jmeter进行webdriver测试 背景:jmeter可以联合selenium进行基本的UI自动化进行测试,解放了手工测试的压力.那么selenium webdriver完成GUI的流程初步如下 ...

  9. 【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)

    一.selenium2.0简述 与一般的浏览器测试框架(爬虫框架)不同,Selenium2.0实际上由两个部分组成Selenium+webdriver,Selenium负责用户指令的解释(code), ...

  10. selenium webdriver (python) 第一版PDF

    前言 如果你是一位有python语言基础的同学,又想通过python+ selenium去实施自动化,那么你非常幸运的找到了这份文档,我也非常荣幸能为你的自动化学习之路带来一丝帮助. 其实,我在sel ...

随机推荐

  1. java selenium webdriver处理JS操作窗口滚动条

    未经作者允许,禁止转载!!! java selenium webdriver处理JS操作窗口滚动条 java selenium webdriver处理JS操作窗口滚动条 import org.open ...

  2. VUE路由去除#问题

    最近自己在写一个vue的小型管理系统,在浏览器中看到的路由都是带有#的,很是不好看.为了解决此问题,大家一般都会想到:mode: 'history'.可是在开发阶段没有问题,但是一旦build打包后, ...

  3. 登陆跳板机每天只输入一次token的方法——ssh clone session

    自从跳板机升级后,无所不在的token让小PE很是恼火,于是有了这篇文章@_@ Linux or Mac篇 在Fedora或者Mac下很简单,修改~/.ssh/config文件,没有的话,就新建一个( ...

  4. 76. Minimum Window Substring(hard 双指针)

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  5. HashMap、HashTable、ConcurrentHashMap的区别

    一.相关概念 1.Map的概念 javadoc中对Map的解释如下: An objectthat maps keys to values . Amap cannot contain duplicate ...

  6. hive支持sql大全(收藏版)

    hive操作数据库还是比较方便的,因此才会有hbase与hive整合.下面我们hive的强大功能吧.为了增强阅读性,下面提几个问题: 1.hive支持哪些运算符? 2.hive是否支持左右连接? 3. ...

  7. linux常用命令:du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项] [文件|目录] 2. ...

  8. python-自定义异常,with用法

    抛出异常 #coding=utf-8 def  exceptionTest(num): if num<0: print "if num<0" raise Excepti ...

  9. python进程、多进程

    进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当 ...

  10. python网络编程之一

    套接字的详细介绍会在另一篇博文指出,此片博文主要是python套接字的简单客户端编写. 两种套接字介绍: 面向连接的套接字:面向连接的套接字提供序列化,可靠的和不重复的数据交付.面向连接是可靠的传输, ...