title: How to handle browser javascript confirm navigation alert with ruby watir script
body: <p>I'd like to programmatically handle the javascript confirmation alert asking if you'd like to leave the page (See screenshot below). </p>
  
  <p><img src="https://i.stack.imgur.com/pFG8D.png" alt="google chrome alert"></p>
  
  <p>The methods that I've tried so far have not worked.</p>
  
  <p>The browser is not recognizing that I am returning the value true for wanting to leave the page.</p>
  
  <p>This is the part where I am trying to handle this alert:</p>
  
  <pre><code>if  @b.div(:class, "infoMessageBox error").present?
          puts "There was an error"
          #@b.execute_script("window.confirm = function() {return true}")
          @b.execute_script("window.onbeforeunload = function() {};")
          @b.goto("#{@env}/TranslateFile.aspx")
          break
      end
  </code></pre>
  
  <p>I have also read this article: "<a href="https://stackoverflow.com/questions/9389379/dismiss-confirm-navigation-popup-with-watir">Dismiss &quot;Confirm Navigation&quot; popup with Watir</a>" but the method in this article does not help in my case.</p>
  
  <p>I would apreciate some help because I'm stuck right here.</p>
  
  <p><strong>THIS HELPED in solving my problem.</strong></p>
  
  <p>I improved the code and now it works better but here might be some other problems in future (hope not).</p>
  
  <pre><code>@b.refresh
  if @b.alert.exists?
      @b.alert.text
      @b.alert.ok 
  end
  @b.goto("#{@env}/TranslateFile.aspx")
  </code></pre>
  
  <p>This helped me and now it escapes the alert and continues the workflow. Thanks for help ! :)</p>
  
  <p><strong>More info for others who might have trouble with handling the alerts ( link located below ):</strong></p>
  
  <pre><code># Check if alert is shown
  browser.alert.exists?
  
  # Get text of alert
  browser.alert.text
  
  # Close alert
  browser.alert.ok
  browser.alert.close
  JAVASCRIPT CONFIRMS
  # Accept confirm
  browser.alert.ok
  
  # Cancel confirm
  browser.alert.close
  JAVASCRIPT PROMPT
  # Enter text to prompt
  browser.alert.set "Prompt answer"
  
  # Accept prompt
  browser.alert.ok
  
  # Cancel prompt
  browser.alert.close
  
  # don't return anything for alert
  browser.execute_script("window.alert = function() {}")
  
  # return some string for prompt to simulate user entering it
  browser.execute_script("window.prompt = function() {return 'my name'}")
  
  # return null for prompt to simulate clicking Cancel
  browser.execute_script("window.prompt = function() {return null}")
  
  # return true for confirm to simulate clicking OK
  browser.execute_script("window.confirm = function() {return true}")
  
  # return false for confirm to simulate clicking Cancel
  browser.execute_script("window.confirm = function() {return false}")
  
  # don't return anything for leave page popup
  browser.execute_script("window.onbeforeunload = null")
  </code></pre>
  
  <p><a href="http://watir.com/guides/alerts/" rel="nofollow noreferrer">Documentation from watir</a></p>