2013-01-21 53 views
0

任何人都可以通過提供小提示提及如何在Mechanize中寫下如何幫助我?我對Gem Mechanize完全陌生。可以使用Selenium驅動描述的代碼在機械化中實現嗎?

require "rubygems" 
require "selenium-webdriver" 

driver = Selenium::WebDriver.for :firefox 
driver.get "https://www.example.com/" 

element = driver.find_element :name => "username" 
element.send_keys "#####" 
element = driver.find_element :name => "password" 
element.send_keys "******" 
element.submit 
element = driver.find_element(:name, "btnHome") 
element.click 
element=driver.find_element(:link, "Empdetals") 
#print element.attribute(:href) 
element.click 
element = driver.find_element :name => "search.empdirectory" 
element.send_keys "#######" 
element = driver.find_element :name => "btnSearch" 
element.click 
driver.current_url 

ERROR當我試圖通過@Prakash提供的「mechanzie`版本

D:\Ruby script>ruby gmail.rb 
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net/http/persist 
ent/ssl_reuse.rb:70:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 rea 
d server certificate B: certificate verify failed (OpenSSL::SSL::SSLError) 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net 
/http/persistent/ssl_reuse.rb:70:in `block in connect' 
     from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:54:in `timeout' 
     from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:in `timeout' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net 
/http/persistent/ssl_reuse.rb:70:in `connect' 
     from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:in `do_start' 
     from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:750:in `start' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net 
/http/persistent.rb:628:in `start' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net 
/http/persistent.rb:570:in `connection_for' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net 
/http/persistent.rb:926:in `request' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize/h 
ttp/agent.rb:258:in `fetch' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r 
b:407:in `get' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r 
b:306:in `click' 
     from gmail.rb:6:in `block in <main>' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r 
b:409:in `get' 
     from gmail.rb:4:in `<main>' 

D:\Ruby script> 
+0

預計您將展示解決此問題的嘗試。堆棧溢出不是代碼轉換或代碼寫入服務。 –

回答

1

是,mechanize gem可用於自動化與網站進行任何交互,包括登錄到網站通過提交用戶名/密碼並點擊提交按鈕/鏈接等。

selenium-webdriver不同,它可以直接調用瀏覽器,mechanize本身充當瀏覽器。

結賬EXAMPLES page on mechanize documentation瞭解如何使用mechanize。第二個示例 - RubyForge - 顯示如何登錄到網站並使用生成的頁面。

有關如何與mechanize退房RailsCasts episode on mechanize

在這裏工作的簡要概述爲開始http://www.google.com,點擊「Gmail的」文本,登錄Gmail,並列出網頁內的鏈接的示例代碼:

require 'mechanize' 

a = Mechanize.new 
a.get('http://www.google.com') do |page| 
    # Click the Gmail link 
    gmail_login_page = a.click(page.link_with(:text => "Gmail")) 

    # Submit the login form 
    gmail_page = gmail_login_page.form_with(:action => 'https://accounts.google.com/ServiceLoginAuth') do |f| 
    f.Email = "<username>@gmail.com" 
    f.Passwd = "**********" 
    end.click_button 

    # List all the links in the personal gmail page 
    gmail_page.links.each do |link| 
    text = link.text.strip 
    next unless text.length > 0 
    puts text 
    end 
end 

希望它有助於開始Mechanize並探索它!

+0

你可以使用'mechanize'對代碼做一點改動嗎?這可以給我一個好的開始。請 –

+0

編輯答案並添加示例。我沒有使用不存在的https:// www.example.com /網站,而是在我的示例中使用了google/gmail。希望能幫助到你! –

+0

感謝您給出了一個好的提示,現在我將用它來實際使用它! –

相關問題