使用watir-webdriver选择本地文件 [英] Selecting local files using watir-webdriver
问题描述
我正在尝试使用watir-webdriver从html页面自动选择本地文件的过程
I'm attempting to automate the process of selecting a local file from a html page using watir-webdriver
我有以下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>
我试图单击id
为test
的输入,并设置要使用watir-webdriver上传的本地文件的路径.
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")
这将导致以下错误
Watir::Exception::UnknownObjectException: unable to locate element, using {:name=>"file", :tag_name=>"input", :type=>"file"}
尝试
@browser.button(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")
导致以下错误
NoMethodError: undefined method `set' for #<Watir::Button:0x3859920>
任何人都可以帮忙吗?我正在努力理解为什么file_field
选项不起作用.
Can anyone help? I'm struggling to understand why the file_field
option doesn't work.
推荐答案
尝试使用以下功能:
@browser.file_field(:id,"upload").set("filepath")
此外,如果您使用的是IE浏览器,请确保使用的是IEDriverServer_Win32_2.33.0,因为它可以在该驱动程序上正常运行,而不是最新版本.
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.
这篇关于使用watir-webdriver选择本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!