Ruby下WebDriver的相關(guān)操作指南大家都了解嗎?我們要不斷的學(xué)習(xí)新的知識(shí),才能在工作中處變不驚,今天武林技術(shù)頻道小編就帶大家進(jìn)入下文學(xué)習(xí),有興趣的朋友一起來(lái)看看吧!
顯性等待:
?
ruby;">wait = Selenium::WebDriver::Wait.new(:timeout => 3)wait.until { driver.find_element(:id => "cheese").displayed? }?
隱性等待:
?
driver = Selenium::WebDriver.for :firefoxdriver.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è)超過60秒時(shí)間的頁(yè)面,你會(huì)看到一個(gè)來(lái)自于Net:HTTP的超時(shí)錯(cuò)誤。你可以在啟動(dòng)瀏覽器前手動(dòng)配置超時(shí)時(shí)間。
?
client = Selenium::WebDriver::Remote::Http::Default.newclient.timeout = 120 # secondsdriver = 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.newprofile.add_extension("/path/to/firebug.xpi")driver = Selenium::WebDriver.for :firefox, :profile => profile?
?于是乎自己嘗試了下,但是呢每次都是提示我firebug.xpi找不到
今天有空倒騰了,問題解決了
其實(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 :ffprofile = Selenium::WebDriver::Firefox::Profile.newprofile.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 => profiledr.get "http://www.baidu.com"?這樣子當(dāng)我們需要查看dom結(jié)構(gòu)時(shí),我們就可以直接在打開的測(cè)試頁(yè)面上調(diào)試?yán)玻挥萌バ麻_個(gè)firefox去查看dom結(jié)構(gòu)了。
以上就是關(guān)于Ruby下WebDriver的相關(guān)操作指南,要想知道更多的技術(shù)知識(shí),大家需要去做多方了解,武林技術(shù)頻道是你的滿意選擇,大家可以放心閱覽。
新聞熱點(diǎn)
疑難解答
圖片精選