顯性等待:
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
內(nèi)部超時(shí):
WebDriver在內(nèi)部使用http協(xié)議與各種driver發(fā)生交互聯(lián)系。默認(rèn)情況下,Ruby標(biāo)準(zhǔn)庫(kù)中的Net::HTTP協(xié)議使用時(shí)有60秒默認(rèn)超時(shí)時(shí)間,如果你調(diào)用Driver去加載一個(gè)超過(guò)60秒時(shí)間的頁(yè)面,你會(huì)看到一個(gè)來(lái)自于Net:HTTP的超時(shí)錯(cuò)誤。你可以在啟動(dòng)瀏覽器前手動(dòng)配置超時(shí)時(shí)間。
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120 # seconds
driver = Selenium::WebDriver.for(:remote, :http_client => client)
ruby webdriver 啟動(dòng)firefox driver時(shí),加載firebug的擴(kuò)展
在官方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找不到
今天有空倒騰了,問(wèn)題解決了
其實(shí)是之前的理解錯(cuò)了,因?yàn)閐r =Selenium::WebDriver.for:ff
啟動(dòng)ff時(shí),都是初始化一個(gè)最簡(jiǎn)單的profile,里面不帶有firebug插件的,也就是說(shuō),哪怕我們?cè)仍趂irefox上面安裝了firebug,也是啟動(dòng)不了的,所以當(dāng)我們需要使用firebug時(shí),才需要加載一個(gè)firebug的擴(kuò)展
profile.add_extension("/path/to/firebug.xpi")
至于“/path/to/firebug.xpi”就是firebug.xpi的存放路徑了,我們可以去網(wǎng)上下載一個(gè)firebug.xpi(對(duì)應(yīng)版本, 我的ff是14,可以使用firebug-1.10.4.xpi,最好使用非firefox瀏覽器下載,不然提示你直接安裝到firefox)
我們可以直接把firefox.xpi存放在我們腳本所存放的路徑,相對(duì)路徑和絕對(duì)路徑都可以
舉個(gè)百度的例子
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存放在與腳本同級(jí)的path/to下面/font>
dr = Selenium::WebDriver.for :firefox, :profile => profile
dr.get "http://www.baidu.com"
這樣子當(dāng)我們需要查看dom結(jié)構(gòu)時(shí),我們就可以直接在打開(kāi)的測(cè)試頁(yè)面上調(diào)試?yán)?,不用去新開(kāi)個(gè)firefox去查看dom結(jié)構(gòu)了。