Search code examples
rubyfileuploadwindowwatir

Watir file upload window preventing from further redirection


When I upload photos using watir with this code on windows:

file_input.set("#{Dir.pwd}/photos/" +'image.jpg')

uploading works okay, but a dialog windows appears, and it prevents the script from redirecting to the next page. I don't know how to close it, and I need to find a way to handle it. browser.windows.size shows that there is only one window, and I'm stuck. Can anyone help me?


Solution

  • It is a Windows dialog, it takes over the focus, you cannot click anything else (Watir goes on, but redirection at the end is aborted).

    I found solution by preventing default behavior of input element:

    browser.execute_script("document.getElementById('file').onclick = function(e){ e.preventDefault() } ")
    

    I don't know if it is good solution but it works fine for me (if not, please notify me), I hope that it would help someone.