[英]Open the default browser in Ruby
在 Python 中,你可以这样做:
import webbrowser
webbrowser.open_new("http://example.com/")
它将在默认浏览器中打开传入的 url
有红宝石等价物吗?
首先,安装Launchy gem:
$ gem install launchy
然后,你可以运行这个:
require 'launchy'
Launchy.open("http://stackoverflow.com")
这应该适用于大多数平台:
link = "Insert desired link location here"
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{link}"
end
仅限 Mac 的解决方案:
system("open", "http://stackoverflow.com/")
要么
`open http://stackoverflow.com/`
最简单的 Win 解决方案:
`start http://www.example.com`
仅限 Linux 的解决方案
system("xdg-open", "http://stackoverflow.com/")
这也有效:
system("start #{link}")
仅 Windows 解决方案:
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute(...)
您可以使用“os” gem: https : //github.com/rdp/os让您的操作系统(在最好的情况下您拥有自己的操作系统,即不是 OS X)决定如何处理 URL。
通常这将是一个不错的选择。
require 'os'
system(OS.open_file_command, 'https://stackoverflow.com')
# ~ like `xdg-open stackoverflow.com` on most modern unixoids,
# but should work on most other operating systems, too.
如果是 windows 和 IE,试试这个: http : //rubyonwindows.blogspot.com/search/label/watir也看看 Selenium ruby: http : //selenium.rubyforge.org/getting-started.html
HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.