有人可以通过提供小提示来帮助我,提及如何在Mechanize
中编写以下内容吗?我对宝石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>
答案 0 :(得分:1)
是的,mechanize
gem可用于自动化与网站的任何互动,包括通过提交用户ID /密码并点击提交按钮/链接等登录网站。
与直接调用浏览器的selenium-webdriver
不同,mechanize
本身充当浏览器。
结帐EXAMPLES page on mechanize documentation以了解如何使用mechanize
。第二个示例 - 使用RubyForge - 显示如何登录站点并使用结果页面。
要快速了解如何使用mechanize
,请查看RailsCasts episode on mechanize
以下是从http://www.google.com开始,点击&#39; Gmail&#39;文本,登录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
并进一步探索它!