Ruby Mechanize, save html as file after filling a form - ruby

I want to save the html after filling a form. lets say:
page.form.field.value = 'testing'
page.save 'test.html'
the generated test.html file don't have the modified value attribute
<input name='something' value=''>
I'm expecting:
<input name='something' value='testing'>

You want to use dom functions for that:
page.at('[name=something]')['value'] = 'testing'
In other words there's no reason to expect that changes to the Form fields will update the dom.

Related

I need to click this input only if it has "aria-checked ="true"

I need to click this input only if it has "aria-checked ="true"
<input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-131-input" tabindex="0" aria-checked="true" style="" xpath="1">
Ruby:
aria_checked = true
if aria_checked = true
impressora_etiqueta = "//mat-checkbox[#id='mat-checkbox-23']/label/div"
page.find(:xpath, impressora_etiqueta).click
end
There are many ways to do what you want - simplest is probably
page.first('#mat-checkbox-23[aria-checked="true"]', minimum: 0)&.click
which will look for the first element with the given id and aria-checked = "true" and click it if one exists.
Note: the id in your test and sample HTML didn't match so I went the id from your test, adjust as needed. Also you have a class of cdk-visually-hidden shown -- If that's actually making the element not visible on the page, then this won't work and you'll need to add more surrounding HTML to your question with a better description of exactly what you're trying to do (you can't click on non-visible elements)

ruby mechanize find and fill input tag value

I need to fill the value of an input tag not surrounded by a form tag, I do not need to submit it, just fill in a value.
I have the code below:
input = my_page.at("#crn_id1")
my_page.form_with(:id => 'crn_id1').value = '90098'
input = agent.click(my_page.at('[#value="Submit Changes"]'))
puts input.content
I am getting an error but what I am trying to do is fill out the input tag which has html of:
<TD style=padding:10px; CLASS="dedefault"><p class="centeraligntext"></p>
<LABEL for=crn_id1><SPAN class="fieldlabeltextinvisible">CRN</SPAN></LABEL>
<INPUT TYPE="text" NAME="CRN_IN" SIZE="8" MAXLENGTH="5" ID="crn_id1">
</TD>
I just want to fill out the input tag which in NO way is surrounded by a form tag with a value of 09987. I have the correct html page but cant seem to set the value of the input and tag to set it and set it in mechanize.

Nokogiri should not include DOCTYPE [duplicate]

This question already has answers here:
How to prevent Nokogiri from adding <DOCTYPE> tags?
(2 answers)
Closed 8 years ago.
I am trying to produce a partial HTML document using Nokogiri, e.g. something along the lines of:
html_content = Nokogiri::HTML::Builder.new() do |doc|
# producing document here, redacted for brevity
end.to_html
This works well enough, except for a little catch: data will later be dispatched to a remote Drupal-powered server and rendered as part of a page and thus should not contain the initial <!DOCTYPE html ...> declaration.
How would I go about convincing Nokogiri not to produce the DOCTYPE tag? Or is Nokogiri's HTML builder the wrong way to go about that?
Thanks in advance.
To achieve this you could use document fragments and the Builder.with method, like this:
require 'nokogiri'
include Nokogiri
fragment = HTML.fragment('')
HTML::Builder.with(fragment) do |f|
f.div('foo')
end
fragment.to_html
# => <div>foo</div>
Nokogiri makes it easy to create templates you can populate on the fly; I'd do it this way:
require 'nokogiri'
DESTINATION_HOST = 'http://www.example.com/some/API/call'
HTML_TEMPLATE = <<EOT
<form method="post">
<input name="user" type="text">
<input name="desc" type="text">
</form>
<div id="quote">
</div>
EOT
doc = Nokogiri::HTML::DocumentFragment.parse(HTML_TEMPLATE)
doc.at('form')['action'] = DESTINATION_HOST
doc.at('div').content = "Danger is my middle name."
[
['user', 'Austin Powers'],
['desc', 'Man of Mystery'],
].each do |name, value|
doc.at("input[name=\"#{name}\"]")['value'] = value
end
puts doc.to_html
# >> <form method="post" action="http://www.example.com/some/API/call">
# >> <input name="user" type="text" value="Austin Powers"><input name="desc" type="text" value="Man of Mystery">
# >> </form>
# >> <div id="quote">Danger is my middle name.</div>
The array and other fields that are populated could easily be loaded from a CSV or YAML file, JSON retrieved on the fly from another host, or directly from a database call.
You know how your document should look beforehand, so use that knowledge to create a template. Nokogiri's Builder is better suited for those times you're not even sure what tags you're going to need and need to dynamically build the entire document structure on the fly.
The hardest part is to define how you're going to loop over various tags in the document to stuff them with content or fill in the parameters, but once you've done that it's easy to create boilerplate you fill in and forward to something else.

How to send many form input values with $.ajax

I have a form with many input elements inside that looks like:
<form>
<input name="foo[abc]" />
<input name="foo[def]" />
...
</form>
(foo should be a array inside $_POST)
How can I send these values trough AJAX, without having to manually enter each value in the data parameter?
use:
var data=$(your_form).serialize();
and send this data using ajax.
it'll automatically convert all array like inputs and normal to a string like get method..
$("form").serialize();
Use the serialize function; see an example here (it's a $.post(), but same difference).

get form values other than by name in codeigniter

hi i am using codeigniter . i have a form , there i add hidden fields dynamically . so every hidden field is <input type='hidden' name='hidden' value="+$(this).attr('title')+"> so the name is equal .
the problem is when i submit the form and try to get my hiden field values i can only get one hidden field value , because the names are same
i print my form values
print_r($this->input->post());
i have 2 hidden fields but i get only one
Array
(
[hidden] => march
[textbox] => march
[mysubmit] => Submit
)
i can change the name dynamically of hidden field when creating , but then i don't know exactly the name of my hidden field ,
how can i get hidden field values with same name ?? is there any way to get form values other than by name ?? i tried and can not find an answer , please help .............
You'll need to use brackets in your name attributes:
<input type='hidden' name='hidden[]'>
<!-- ^^^^ -->
This will allow PHP to accept multiple inputs with the same name as an array of values, so in this case, $_POST['hidden'] will return an array of strings.
By default they are indexed starting at 0, so $_POST['hidden'][0] will get you the first one, $_POST['hidden'][1] will get you the second, etc., however - you can explicitly index them if it's easier for you, either with numbers or strings.
<input type='hidden' name='hidden[first]'>
<input type='hidden' name='hidden[second]'>
Or:
<input type='hidden' name='hidden[0]'>
<input type='hidden' name='hidden[1]'>
You can nest these as deep as you want like hidden[first][1][], and they will be treated similarly to a PHP array when you get the $_POST values, but you need the brackets in the HTML.
Without brackets, only the last field's value will be available in the $_POST array. This is a PHP feature, Codeigniter can't do anything about it.

Resources