ruby 中使用 Dom技术来查找页面元素
有些时候可能会根据一些有限的信息,来查找页面的元素,这里举一个例子利用页面文字来查找所在的标签,以淘宝的登录页面为例,使用以下代码可以实现根据账户名来识别对应的节点名称:
require ‘watir’
#ie = Watir::IE.attach(:url, /member1.taobao.com/)
ie = Watir::IE.start(”http://member1.taobao.com/member/login.jhtml?ssl=false”)
def find_tag_by_text(ie,textName)
all_elements = ie.document.body.all
all_elements.each do |object|
if object.innerText.match(textName)
flag = false
all_children = object.children
#如果子对象匹配到,那么退出,退出后flag为true
all_children.each do |child|
if child.innerText.match(textName)
flag = true
break
else
next
end
end
# 当子节点匹配不到text时,那么表示已经查到了终点
unless flag
puts object.tagName
break
end
end
end
end
find_tag_by_text(ie,”账户名”)
如果要得到该节点的父节点,可以写成:
unless flag
parent = object.parentElement
puts parent.tagName
break
end
如果要得到该节点子节点,可以写成:
unless flag
children = object.children
puts “_____________”
children.each do |child|
pu
相关文档:
下文转自:
http://www.cnblogs.com/watir/archive/2009/04/25/1443440.html
ruby文件从命令行中接收参数
在命令行方法执行ruby文件时,需要从命令行中传入参数,可以使用全局变量:ARGV
如有ruby 文件test.rb,内容如下:
1 def hello(a)
2 puts a
3 end
4
5 ......
转自 http://zhujg.javaeye.com/blog/355040
首先 安装 cygwin
cygwin 要安装的插件是
默认的+ make + gcc + libiconv
+ openssl
cygwin下需要编译原文件
到ruby-lang(http://ruby-lang.org/)
下载ruby-1.9.1-p0.tar.gz
tar xvf ruby-1.9.1-p0.tar.gz
cd
ruby-1.9.1-p0
./configure
make && make in ......
转自51testing.com, 原见:http://bbs.51testing.com/thread-171535-1-1.html http://swik.net/Watir+Programming
http://www.pragprog.com/ #很多好的源码哦
http://docs.rubygems.org/ #rubygems
http://www.fxruby.org/ #fxruby
http://groups.google.com/group/watir-general/topics #goog ......
Installing Ruby from source is my preferred method, although in Ubuntu Feisty you can supposedly install it with apt-get install ruby
now. Here’s the essential packages needed to get a source build working right though and the process I just went through:
sudo apt-get install build-essentia ......