在ruby中打开填充表单页面

问题描述 投票:0回答:2

我正在使用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 = "[email protected]"
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或类似的方法。有没有办法实现这一点(使用机械或其他宝石)?

ruby nokogiri mechanize
2个回答
1
投票

正如其他人在评论中提到的那样尝试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 '[email protected]'

binding.pry # or sleep 60 
driver.quit

有关更多说明see documentation


2
投票

这不起作用,因为设置表单字段甚至不更新DOM。

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

© www.soinside.com 2019 - 2024. All rights reserved.