selenium-webdriver 簡單教程

來源:互聯網
上載者:User

標籤:uil   _for   並且   alert   orm   navigate   data-   ted   執行個體   

ruby環境下selenium/webdriver可以通過selenium-webdriver.gem包進行安裝

gem install selenium-webdriver 支援語言及版本有ruby 1.8.7~1.9.2,jrbuy和rubinius selenium-webdriver包含了selenium-client,在閱讀的時候,要注意它們兩個命名空間是在兩不同的API裡:1.Selenium::WebDriver - WebDrver API2.Selenium::Client - Selenium RC API WebDrver API是繼承自Selenium RC API,所以沒有必要在Selenium RC API花大量的時間,我們可以直接從Selenium::WebDriver開始,並圍繞兩個大類:Seleniu::WebDriver:Driver和Selenium::WebDriver::Element,這是整個WebDriver API的入口。 API 例子:一個簡單的例子:require "selenium-webdriver" driver = Selenium::WebDriver.for :firefoxdriver.navigate.to "http://www.google.com.hk" element = driver.find_element(:name,‘q‘)element.send_keys "Hello WebDriver"element.submit puts driver.title driver.quit Driver 例子:# 應用javascriptputs driver.execute_script("return window.location.pathname") # 利用ruby和js擷取元素element = driver.execut_script("return document.body")driver.execut_script("return arguments[0].tagname", element) #=> "BODY" # 等待一些特殊元素的出現wait = Selenium::WebDriver::Wait.new(:timeout=>10) # secondswait.until{driver.find_element(:id,"foo")}# 註:wait在new時,可以設定三個值,分別為:timeout(預設5秒),:message(預設nil),:interval(預設0.5) # 選擇framedriver.switch_to.frame "some-frame" # name或iddriver.switch_to.frame driver.find_element(:id, ‘some-frame‘) # frame# 註:switch_to方法不僅可以選擇frame,還可以處理window,alert,comfirmation等視窗 # 選擇回主視窗driver.swith_to.default_content Element 例子:# 擷取元素屬性class_name = element.attr ibute("class")# 判斷元素是否顯示element.displayed? # 擷取元素在頁面上的相對座標位置element.locationelement.location.xelement.location.y # 將元素滾動到視頻可以顯示的位置,再返回元素的相對座標element.location_once_scrolled_into_view # 擷取元素的寬和高element.size # 在元素裡輸入空,參看Selenium::WebDriver::Keys輸入值element.send_keys :spaceelement.send_keys "tet", :arrow_left, "s" #=> "test", 先輸入 tet, 再輸入一次左方向鍵,再輸入selement.send_keys [:control, ‘a‘], "1" #=> "1", 先輸入crtl+a,再輸入1  # 擷取元素文本element.text  更進階的用法(見 ActionBuilder)driver.action.key_down(:shift).click(element).double_click(second_element).key_up(:shift).drag_and_drop(element,third_element).perform  啟動chrome瀏覽器的方法 1.下載ChromeDriver並運行, 2.啟動chromedriver = Selenium::WebDriver.for :remote, :url=>"http://localhost:9515"driver.get "http://www.google.com.hk"其它操作一樣。 如果這樣感覺比較麻煩,可以將下載的chromedriver.exe路徑載入到環境變數中,就可直接用了driver = Selenium::WebDriver.for :chrome  chrome個人化啟動profile = Selenium::WebDriver::Chrome::Profile.newprofile[‘download.prompt_for_download‘] = falseprofile[‘download.default_directory‘] = "d:/download" driver = Selenium::WebDriver.for :chrome, :profile=>profile Remote應用RomteWebDriver可以控制瀏覽器在不同的機器上運行,下載selenium-server-standlone-x.xx.x.jarjava -jar selenium-server-standalone.jar ‘啟動服務 driver = Selenium::WebDriver.for :remote 預設情況下,可以啟動服務運行在localhost:4444,並開啟firefox。如果想串連其它機器上的服務,可以用:url選項driver = Selenium::WebDriver.for :remote, :url=>"http://remoteserver:44444/wd/hub" 本機不用加/wd/hub,但遠程一定要加 啟動其它的瀏覽器,用:desired_capabilities選項driver = Selenium::WebDriver.for :remote, :desired_capabilities=>:chrome  Selenium::WebDriver::Remote::Capabilities的例子 require "selenium-webdriver"include Selenium caps = WebDriver::Remote::Capabilities.htmlunit :javascript_enabled=>truedriver = WebDriver.for :remote, :desired_capalibities=>caps   修改remote服務的連接埠號碼,目前只支援firefox include Selenium caps = WebDriver::Remote::Capabilities.firefox (:proxy=>WebDriver::Proxy.new(:http=>"myproxyaddress:8888"))driver = WebDriver.for :remote, :desired_capalibities=>caps 如果是一個遠端remote服務 include Selenium client = WebDriver::Remote::Http::Default.new client.proxy = Proxy.new :http=>"proxy.org:8888"driver = WebDriver.for :remote, :http_client=>client  Firefox加擴充外掛程式在用firefox時,經常要用到firebug進行查看,啟動firefox帶firebuginclude Selenium profile = WebDriver::Firefox::Profile.newprofile.add_extension "path/firebug.xpi" driver = WebDriver.for :firefox, :profile=>profile 使用已經存在的profile使用一個已經存在的profile模板,可以用firefox -profilemanger把profile儲存出來(這個命令在ff8上似乎無用,還要進一步驗證) driver = Selenium::WebDriver.for :firefox, :profile=>"my_existing_profile" 如果想用預設profile,可以通過 :profile=>"default" 或者也可以這通過profile執行個體來使用已經存在的和自訂的profile。此方法不能修改已經存在的profile,且只能在webdriver下使用 default_profile = Selenium::WebDriver::Firdfox::Profile.from_name "default"default_profile.native_events = true driver = Selenium::WebDriver.for :firefox, :profile=>default_profile 匯出firefox profile見《Firefox 8匯出profile檔案》 Firefox個人化連接埠號碼profile = Selenium::WebDriver::Firefox::Profile.newprofile[‘browser.download.dir‘] = ‘d:\download‘profile[‘browser.download.folderlist‘] = 2profile[‘browser.helperApps.neverAsk.saveToDisk‘] = "application/pdf" driver = Selenium::WebDriver.for :firefox, :profile=>profile 用remote driver啟動時,任然可以對firefox進行個人化 profile = Selenium::WebDriver::Firfox::Profile.newprofile[‘foo.bar‘] = true capabilities = Selenium::WebDriver::Remote::Capabilities.firefox :firefox_profile=>profiledriver = Selenium::WebDriver.fox :remote, :desired_capabilities=>capabilities 設定Firefox路徑當firfox不是安裝在預設路徑中時,要設定firefox的安裝路徑Selenium::WebDriver::Firefox.path = "/path/to/firefox" # firefox安裝路徑driver = Selenium::WebDriver.for :firefox  本地事件本地事件在window下預設是啟用的,可以設定關閉:profile = Selenium::WebDriver::Firefox::Profile.newprofile.native_events = false # 關閉 driver = Selenium::WebDriver.for :firefox, :profile=>profile 理論上linux支援本地事件,profile.native_events = true # 開啟  Operaopera也是用remote的方法進行啟動的。在開始之前,要下載selenium-server-standalone-x.xx.x.jar,並且建立環境變數SELENIUM_SERVER_JAR到本地系統中。window: SELENIUM_SERVER_JAR=..\server-standalone.jar liunx: export SELENIUM_SERVER_JAR=path_to/server-standalone.jar  便可以輕鬆的用Selenium::WebDriver啟動Opera driver = Selenium::WebDriver.for :operadriver.navigate.to "http://www.baidu.com" Timeouts 隱式等待WebDriver可以設定隱式等待,所以在應用#find_element定位元素時,會等待元素的出現,直至NoSuchElementError的出現。 driver = Selenium::WebDriver.for :firefoxdriver.manager.timeouts.implicit_wait = 3 # 等待3秒 顯示等待可以用wait類去執行個體化一些等待條件 wait = Selenium::WebDriver::Wait.new(:timeout=>3)wait.until {driver.find_elenium(:id=>"foo").displayed?}  內部的逾時WebDriver使用了大量的HTTP的驅動(jsonWireProtocol)。Net::HTTP作為ruby的標準庫使用,預設逾時為60秒。如果用WebDriver#get啟動一個載入時間超過60秒的頁面,你會看Net::HTTP的TimeoutError錯誤,可以在啟動瀏覽器之前修改timeout來改變預設逾時時間長度。 client = Selenium::WebDriver::Remote::Http::Default.newclient.titmeout = 120 # 設定為120秒driver = Selenium::WebDriver.for :temote, :http_client=>client js彈出框擷取js的alert,prompt和comfirm彈出窗都是用switch_to  require "selenium-webdriver" driver = Selenium::WebDriver.for :firefoxdriver.navigate.to "http://mysite.com/page_with_alert.html" driver.find_element(:name, ‘element_with_alert_javascript‘).clicka = driver.switch_to.alertif a.text == ‘A value you are looking for‘  a.dismisselse  a.acceptend  用Curb或者自己的http clientHTTP通訊內部預設使用Net::HTTP,如果有安裝Curb gem就可以選擇這麼做 require "selenium/webdriver/remot/http/curb"include Selenium client = WebDriver::Remote::Http::Curb.newdriver = WebDriver.for :remote, :http_client=>client

selenium-webdriver 簡單教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.