0

任何人都可以通过提供一些小提示来帮助我,说明如何编写以下内容Mechanize?我对 Gem 完全陌生Mechanize

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

错误当我尝试@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>
4

1 回答 1

1

是的,mechanizegem可用于自动化与网站的任何交互,包括通过提交用户 ID/密码和单击提交按钮/链接等方式登录网站。

selenium-webdriver直接调用浏览器不同,mechanize它本身充当浏览器。

请查看 mechanize 文档上的示例页面以了解如何mechanize使用。第二个示例 - 使用RubyForge - 展示了如何登录到站点并使用生成的页面。

有关如何使用的快速概览,mechanize请查看RailsCasts 的 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并进一步探索它!

于 2013-01-21T15:20:56.137 回答