Selecting local files using watir-webdriver

2019-06-24 19:30发布

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.

3条回答
爷、活的狠高调
2楼-- · 2019-06-24 19:51

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)
查看更多
Anthone
3楼-- · 2019-06-24 20:01

Try this:

@browser.file_field(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")
查看更多
倾城 Initia
4楼-- · 2019-06-24 20:06

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.

查看更多
登录 后发表回答