问题
I'm doing a bit of scraping on this website: http://onthemap.ces.census.gov/
Here's the setup to my code that precedes the error:
sample_address = "1600 Amphitheatre Pkwy, Mountain View, CA 94043"
b= Watir::Browser.new :chrome
b.goto "http://onthemap.ces.census.gov"
b.text_field(:id => "search_value").set sample_address
b.span(:id => "search_button_label").click
Watir::Wait.until { b.a(:text => "Geocoder Results").exists? }
address_selector = sample_address.split(" ")[0..1].join(" ")
b.a(:text => /#{address_selector}/).click
Watir::Wait.until { b.div(:class => "olPopup").exists? }
b.span(:text => Selection").click
So now I am about to try to plug in 2 miles into the "simple radius" box, whose DEFAULT VALUE is 0.25
b.text_field(:name => "simple_r").set 2.0
The text box changes to 2.0 as expected. Then I try to click to confirm the selection....
b.span(:text => "Confirm Selection").click
Now, all of a sudden the "simple_r" value reverts back to its previous value (0.25) and my script is useless.
To illustrate, if I keep my watir object connected to the browser, and I then proceed to manually type in "100.0" into the Simple Ring radius field, THEN pull up the watir object and run this code again:
b.text_field(:name => "simple_r").set 2
b.span(:text => "Confirm Selection").click
The value for the "simple_r" radius will revert back to 100.
Here's a little screencast showing the error. Note in the screencast I'm not clicking any buttons or entering any text manually. You'll need to switch it to 720p to see properly. http://youtu.be/gdDZWHwXduA
回答1:
It seems like there should be JavaScript that triggers on the blur of the simple radius field. However, it does not seem to be firing consistently. Manually firing the JavaScript addressed the issue:
b.text_field(:name => "simple_r").value = 2
b.text_field(:name => "simple_r").fire_event(:onblur)
Note that I was only able to consistently reproduce the problem in Firefox. I was not able to test the fix in Chrome as the problem never occurred.
来源:https://stackoverflow.com/questions/37145927/watir-text-box-input-not-persisting-after-submitting-form-resetting-to-previous