简体   繁体   中英

Selecting local files using watir-webdriver

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.

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.

试试这个:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM