顯性等待:
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結構了。