手机
当前位置:查字典教程网 >脚本专栏 >ruby专题 >Ruby下WebDriver的相关操作指南
Ruby下WebDriver的相关操作指南
摘要:显性等待:wait=Selenium::WebDriver::Wait.new(:timeout=>3)wait.until{driver....

显性等待:

wait = Selenium::WebDriver::Wait.new(:timeout => 3) wait.until { driver.find_element(:id => "cheese").displayed? }

隐性等待:

driver = Selenium::WebDriver.for :firefox driver.manage.timeouts.implicit_wait = 3 # seconds

内部超时:

WebDriver在内部使用http协议与各种driver发生交互联系。默认情况下,Ruby标准库中的Net::HTTP协议使用时有60秒默认超时时间,如果你调用Driver去加载一个超过60秒时间的页面,你会看到一个来自于Net:HTTP的超时错误。你可以在启动浏览器前手动配置超时时间。

client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 120 # seconds driver = Selenium::WebDriver.for(:remote, :http_client => client)

ruby webdriver 启动firefox driver时,加载firebug的扩展

在官方wiki上看到

Adding an extension

It's often useful to have Firebug available in the Firefox instance launched by WebDriver:

profile = Selenium::WebDriver::Firefox::Profile.new profile.add_extension("/path/to/firebug.xpi") driver = Selenium::WebDriver.for :firefox, :profile => profile

于是乎自己尝试了下,但是呢每次都是提示我firebug.xpi找不到

今天有空倒腾了,问题解决了

其实是之前的理解错了,因为dr =Selenium::WebDriver.for:ff

启动ff时,都是初始化一个最简单的profile,里面不带有firebug插件的,也就是说,哪怕我们原先在firefox上面安装了firebug,也是启动不了的,所以当我们需要使用firebug时,才需要加载一个firebug的扩展

profile.add_extension("/path/to/firebug.xpi")

至于“/path/to/firebug.xpi”就是firebug.xpi的存放路径了,我们可以去网上下载一个firebug.xpi(对应版本, 我的ff是14,可以使用firebug-1.10.4.xpi,最好使用非firefox浏览器下载,不然提示你直接安装到firefox)

我们可以直接把firefox.xpi存放在我们脚本所存放的路径,相对路径和绝对路径都可以

举个百度的例子

require 'selenium-webdriver' #dr = Selenium::WebDriver.for :ff profile = Selenium::WebDriver::Firefox::Profile.new profile.add_extension("path/to/firebug-1.10.4.xpi") <font color="DarkOrchid">#firefox-1.10.4.xpi存放在与脚本同级的path/to下面</font> dr = Selenium::WebDriver.for :firefox, :profile => profile dr.get "http://www.baidu.com"

这样子当我们需要查看dom结构时,我们就可以直接在打开的测试页面上调试啦,不用去新开个firefox去查看dom结构了。

【Ruby下WebDriver的相关操作指南】相关文章:

Ruby中操作文件的方法介绍

Ruby中处理时间的一些基本操作

Ruby中操作字符串的一些基本方法

Ruby中一些常用的文件操作方法小结

详解Lua中的表的概念及其相关操作方法

Ruby的安装与运行

Ruby中访问SQL Server数据库的配置实例

简要解读Ruby面向对象编程中的作用域

在Ruby on Rails上使用Redis Store的方法

深入剖析Ruby设计模式编程中对命令模式的相关使用

精品推荐
分类导航