4

I'm attempting to automate the process of selecting a local file from a html page using watir-webdriver

I have the following html

<body>
<form method="post" action="upload" enctype="multipart/form-data">
test file to upload: <input type="file" name="file" size="60" id="test"/>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>

I'm attempting to click the input with an id of test and set a path to the local file I wish to upload using watir-webdriver.

I can use the following to click the button to bring up the selection window using

@browser.goto 'http://www.test.com'
@browser.button(:id => 'test').click

however, i'm trying to use the following (from researching, this seems the correct way. not working though)

@browser.file_field(:name => 'file').set("C:\\path\\to\\test\\file\\validTest.xml")

which results in the following error

Watir::Exception::UnknownObjectException: unable to locate element, using {:name=>"file",    :tag_name=>"input", :type=>"file"}

trying

@browser.button(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")

results in the following error

NoMethodError: undefined method `set' for #<Watir::Button:0x3859920>

Can anyone help? I'm struggling to understand why the file_field option doesn't work.

user3927287
  • 193
  • 1
  • 4
  • 13
  • 1
    If you're getting `UnknownObjectException`, then try `@browser.file_field(:name => 'file').exists?`. If `false`, then it's possible the element isn't really locatable (e.g. in frame, loading asynchronously, etc.). And the [`Button`](http://www.rubydoc.info/gems/watir-webdriver/Watir/Button) class doesn't have a `.set` method, so that error is legitimate. – orde Dec 19 '14 at 17:58

3 Answers3

5

Try using the following function:

@browser.file_field(:id,"upload").set("filepath")

Also, if you are using IE browser then make sure you are using IEDriverServer_Win32_2.33.0 as it works fine on this driver not on latest one.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Rahul
  • 66
  • 2
1

Try this:

@browser.file_field(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")
hon2a
  • 7,006
  • 5
  • 41
  • 55
Oleksandr Holubenko
  • 4,310
  • 2
  • 14
  • 28
  • returns `Watir::Exception::UnknownObjectException: unable to locate element, using {:id=>"test", :tag_name=>"input", :type=>"file"}` – user3927287 Dec 19 '14 at 09:04
  • @user3927287, ok, try this, also `@browser.element(:xpath => '//input[@type="File"]')` – Oleksandr Holubenko Dec 19 '14 at 10:08
  • returns `NoMethodError: undefined method "set" for #` – user3927287 Dec 19 '14 at 11:05
  • @user3927287, I'm sorry, this: `@browser.file_field(:xpath => '//input[@type="File"]')` – Oleksandr Holubenko Dec 19 '14 at 11:13
  • thanks for the quick replies: `@browser.file_field(:xpath => '//input[@type="File"]')` returns `Watir::Exception::UnknownObjectException: unable to locate element, using {:xpath=>"//input[@type=\"File\"]", :tag_name=>"input", :type=>"file"}` and `@browser.file_field(:xpath => '//input[@type="file"]')` returns `Watir::Exception::UnknownObjectException: unable to locate element, using {:xpath=>"//input[@type=\"file\"]", :tag_name=>"input", :type=>"file"}` would the issue be with the HTML? – user3927287 Dec 19 '14 at 11:47
  • @user3927287, I don't think so, but, HTML that you send - 100% correct? – Oleksandr Holubenko Dec 19 '14 at 12:21
  • @user3927287, also, find this variant, `@browser.find_element(:name, "file").send_keys("C:\\path\\to\\test\\file\\validTest.xml")` – Oleksandr Holubenko Dec 19 '14 at 12:25
0

Try using like this in latest IEDriver. assign file path to variable and then set it

filepath = "C:\\path\\to\\test\\file\\validTest.xml"
@browser.file_field(:id,"upload").set(filepath)
Dmitriy
  • 5,525
  • 12
  • 25
  • 38