How to upload a file with watir and IE?

本小妞迷上赌 提交于 2019-11-29 06:57:43

I knew about that problem, and completely forgot! Go to input_elements.rb file in your gems directory, and add the title of the file upload window in your language to POPUP_TITLES (line 443).

Example:

  • before

    POPUP_TITLES = ['Choose file', 'Choose File to Upload']
    
  • after

    POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
    

I installed windows xp in english now, and it works! (The error occured on a localised windows server 2003)

I guess it was the localisation issue. I will just run watir on the english computer from now on.

Constantijn Blondel

I had the same problem today (March1,2012) and landed here via Google.

Thanks Željko for pointing me in the right direction, however the solution of changing the [POPUP_TITLES] didn't work. In fact, this array seems not to exist anymore in the current version of the gem (watir-2.0.4), or maybe I just misread.

I solved the problem in watir-2.0.4/lib/watir/dialogs/file_field.rb: Here, the various window and button titles are defined as regular expressions. Change the regexps in the following methods

  • open_button()
  • cancel_button()
  • file_upload_window()

to match your localized window names. After reloading the gem, it worked flawlessly.

I would suggest that you take a look at FileField#set in input_elements.rb (in your Ruby gems directory), and change sleep 1 to sleep 2 (or some higher number). I have noticed that on slower machines it takes more than a second for file upload pop up to appear.

  @modal = @browser.driver.switch_to.alert   #Switch to open windows modal
  key_to_send = "C:\\Users\\singhku\\Calabash_doc.pdf"  #Path and name of file
  @modal.send_keys(key_to_send)

  require 'win32ole'
  wsh = WIN32OLE.new('Wscript.Shell')
  wsh.AppActivate('Choose File to Upload')  #Name of the modal that is open
  wsh.SendKeys('{ENTER}')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!