当前位置: 移动技术网 > IT编程>脚本编程>Ruby > Ruby下WebDriver的相关操作指南

Ruby下WebDriver的相关操作指南

2017年12月08日  | 移动技术网IT编程  | 我要评论

显性等待:
 

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结构了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网