简体   繁体   English

使用watir-webdriver选择本地文件

[英]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 我正在尝试使用watir-webdriver自动化从html页面中选择本地文件的过程

I have the following html 我有以下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. 我正在尝试单击idtest的输入,并使用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. 我很难理解为什么file_field选项不起作用。

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. 此外,如果您使用的是IE浏览器,请确保您使用的是IEDriverServer_Win32_2.33.0,因为它在此驱动程序上运行正常而不是最新的驱动程序。

试试这个:

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

Try using like this in latest IEDriver. 尝试在最新的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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