Ruby 上述使用Selenium驱动程序的代码描述能否在mechanize中实现?

Ruby 上述使用Selenium驱动程序的代码描述能否在mechanize中实现?,ruby,mechanize,ruby-1.9.3,Ruby,Mechanize,Ruby 1.9.3,有谁能帮我介绍一下如何用Mechanize编写下面的小技巧吗?我对GemMechanize一无所知 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

有谁能帮我介绍一下如何用
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>
D:\Ruby script>Ruby gmail.rb
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net/http/persistent
ent/ssl\u reuse.rb:70:in'connect':ssl\u connect返回=1 errno=0 state=SSLv3 rea
d服务器证书B:证书验证失败(OpenSSL::SSL::SSLError)
来自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'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent/ssl_reuse.rb:70:in'connect'
来自C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:in'do_start'
从C:/Ruby193/lib/ruby/1.9.1/net/http.rb:750:in'start'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:628:in'start'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:570:in'connection_for'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:926:in'request'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize/h
ttp/agent.rb:258:in'fetch'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:407:get中的'get'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:306:在“点击”中
来自gmail.rb:6:in'block in'
来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:409:get中的'get'
来自gmail.rb:4:in`'
D:\Ruby脚本>
是,可用于自动与网站进行任何交互,包括通过提交用户ID/密码和单击提交按钮/链接等方式登录网站

与直接调用浏览器的selenium webdriver不同,
mechanize
本身充当浏览器

执行签出以了解如何使用
机械化
。第二个示例-with-显示了如何登录到站点并使用结果页面

有关如何使用
mechanize
的快速概述,请查看

下面是一个示例代码,用于从开始,单击“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
要求“机械化”
a=机械化
a、 得到('http://www.google.com"做|页|
#点击Gmail链接
gmail\u login\u page=a.点击(page.link\u with(:text=>“gmail”))
#提交登录表单
gmail\u page=gmail\u登录\u page.form\u with(:action=>'https://accounts.google.com/ServiceLoginAuth“)do | f|
f、 电子邮件=“@gmail.com”
f、 Passwd=“**********”
结束。单击“”按钮
#列出个人gmail页面中的所有链接
gmail|u page.links.each do| link|
text=link.text.strip
下一步,除非text.length>0
放置文本
结束
结束

希望它有助于开始使用
Mechanize
,并进一步探索它

您可以使用
mechanize
对代码进行一些更改吗?这可以给我一个好的开始。请填写答案并添加一个示例。而不是使用不存在的
https://www.example.com/
site,我在示例中使用了google/gmail。希望有帮助!谢谢你给了我一个很好的建议,现在我要用它来我的实际一个!我运行代码时出错。你能看一下同样的吗?看看那个问题。最好先尝试mechanize文档中的其他(较小)示例,以熟悉概念。预计您将展示解决问题的尝试。堆栈溢出不是代码转换或代码写入服务。