问题
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.
回答1:
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.
回答2:
Try this:
@browser.file_field(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")
回答3:
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)
来源:https://stackoverflow.com/questions/27554581/selecting-local-files-using-watir-webdriver