0
votes

I am using web-watir to drive phantomjs. I am trying to submit a bunch of forms(POST) on the webpage. I have all the forms in a collection. When I click the submit button and browser.back(), I get Selenium::WebDriver::Error::StaleElementReferenceError. I tried using form.submit() but same issue of going back and getting StaleElementReferenceError. I tried to submit the form in another page (browser.execute_script( "window.open(page)" )) but I don't think that will submit my form (if the form was a GET, that might have worked). I tried Net::HTTP.post_form() which is separate from the phantomjs session (did not work, I need to be logged in).

I am out of ideas except find the elements all over each time I navigate back. I guess I could replace phantomjs with Chrome or Firefox. Tell me, is there a way to submit the form in a new page phantomJS?

Here is some code:

forms = browser.forms()
forms.each{ | form | 
  form.submit()

  browser.back()
}
1

1 Answers

1
votes

If submitting each form brings you to another page, then it is expected that you get a StaleElementReferenceError for any previously saved elements. This is how Selenium-WebDriver was designed, so the problem will still exist if you switch to Chrome or Firefox.

Given that you are iterating through each form, it would be easy to locate each form by index:

browser.forms.length.times do |i|
  browser.form(index: i).submit 
  browser.back
end

The above code took the same approach of directly submitting the form. However, if possible, the submit button should be clicked. Directly submitting the form may bypass important code that is tied to the action of clicking the submit button.