【问题标题】:Open filled-form page in ruby在 ruby​​ 中打开填充表单页面
【发布时间】:2019-02-22 14:57:28
【问题描述】:

我正在使用mechanize 填写表格,但我想在提交之前在网页上查看它。目标是使用预先填写的表单打开浏览器。

require 'mechanize'

mechanize = Mechanize.new
page = mechanize.get('https://www.linuxtoday.com/contribute.html')

form = page.form_with :name => 'upload'
form.sub_name = "mbb"
form.email = "mbb@mbb.com"
form.author_name = "Mr Potatohead"
form.title = "Mr Potatohead writes Ruby"
form.link = "https://potato.potato"
form.body = "More text"

`open #{page.uri}`

调用操作系统打开站点当然是空表单。我没有看到page.open 或类似的方法可用。有没有办法实现这一点(使用机械化或其他宝石)?

【问题讨论】:

  • 不使用 Mechanize,也不通过打开 URL。可以这样想:当您在浏览器中填写表单时,您输入的任何信息是否都包含在 URL 中?如果您填写表格然后复制 URL 并将其粘贴到另一个选项卡中,您输入的信息是否会保留?在这个网站和大多数其他网站的情况下,不,当您使用 Mechanize 时也是如此。如果您想在浏览器窗口中填写表单,您需要使用 Webdriver 或 Chrome 的 Puppeteer 协议之类的东西,它们会引导浏览器本身执行操作。
  • 感谢您提及@JordanRunning。我要求中断客户端-服务器连接,同时解决我无法通过 API 参数化交互的事实。也许我会从命令行而不是在浏览器中以交互方式查看它们......陷阱将围绕验证码工作。
  • 为此,您可能需要制作一个简单的Electron 应用程序。
  • selenium 类似于 mechanize,但使用的是真正的浏览器(无头浏览器或可视浏览器)。您可以在脚本中放置一个“等待”调用以暂停,直到您填写验证码。

标签: ruby nokogiri mechanize


【解决方案1】:

这是行不通的,因为设置表单字段甚至不会更新 DOM。

如果您想查看表单数据,可以检查form.request_data

【讨论】:

    【解决方案2】:

    正如其他人在 cmets 中提到的尝试 selenium,您需要安装 chrome 或 firefox 驱动程序,以下是 chrome 的示例以帮助您入门:

    require 'selenium-webdriver'
    require 'pry' # optional
    
    driver = Selenium::WebDriver.for :chrome
    
    driver.navigate.to 'https://www.linuxtoday.com/contribute.html'
    form = driver.find_element(id: 'upload')
    form.find_element(id: 'sub_name').send_keys 'mbb'
    form.find_element(id: 'email').send_keys 'mbb@mbb.com'
    
    binding.pry # or sleep 60 
    driver.quit
    

    更多说明see documentation

    【讨论】:

    • 这看起来是最有帮助的选择,并给了我一个浏览器来编辑它。谢谢!
    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多