水豚找不到文件输入字段来附加文件 [英] capybara can not find file input field to attach file
问题描述
我使用红宝石,黄瓜和水豚。
I use ruby, cucumber and capybara.
我的html看起来像这样:
My html looks like this:
<tr>
<th>
Promotion codes
</th><td><div class="t-zone" id="promocodesZone">
<span style="display: inline-block;">
<span class="field"><label class="field-label">File name:
<span style="text-transform: none;" class="status-ok"></span></label></span>
<span class="field"><label class="field-label">Codes available:
<span style="text-transform: none;" class="status-ok">0</span></label></span>
<span class="field"><label class="field-label">Codes used:
<span style="text-transform: none;" class="status-ok"></span></label></span>
<span class="field"><label class="field-label">Codes uploaded on:
<span style="text-transform: none;" class="status-ok"></span></label></span>
</span>
<div style="margin-bottom: 15px;"><div class="t-upload">
<div id="promocodesUpload">
<div class="qq-uploader">
<div class="qq-upload-drop-area" style="display: none;"><span>Drop </span></div>
<a class="qq-upload-button btn" style="position: relative; overflow: hidden; direction: ltr;">Upload
<input type="file" name="file" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0;"></a>
<ul class="qq-upload-list"></ul></div></div>
</div>
</div>
</div>
</td>
</tr>
基本上我需要做的是通过单击链接上传来上传文件:
Basically what I need to do is to upload a file by clicking on link "Upload":
<a class="qq-upload-button btn" style="position: relative; overflow: hidden; direction: ltr;">Upload<input type="file" name="file" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0;"></a>
但是我一直失败,因为水豚无法找到必要的元素...
我已经尝试了很多事情:
But I keep failing because capybara is unable to find the necessary element... I have tried many things already:
attach_file('//*[@id="promocodesUpload"]/div/a/input', File.absolute_path($campaign_promotional_code_path))
结果:无法找到文件字段 // * [@ id = \ promocodesUpload\] / div / a / input(Capybara :: ElementNotFound)
Result: Unable to find file field "//*[@id=\"promocodesUpload\"]/div/a/input" (Capybara::ElementNotFound)
和
within(:xpath, '//*[@id="promocodesUpload"]/div/a') do
attach_file("input", File.absolute_path($campaign_promotional_code_path))
end
结果:无法找到文件字段输入(Capybara :: ElementNotFound)
Result: Unable to find file field "input" (Capybara::ElementNotFound)
和
attach_file("Upload", File.absolute_path($campaign_promotional_code_path)
结果:无法找到文件字段上传( Capybara :: ElementNotFound)
Result: Unable to find file field "Upload" (Capybara::ElementNotFound)
和
click_link("Upload")
结果:无法找到链接上载(Capybara :: ElementNotFound)
Result: Unable to find link "Upload" (Capybara::ElementNotFound)
和
有任何帮助吗? :)
推荐答案
尝试一下:
page.driver.browser.all(:xpath, '//input[@name="file"]')[0].send_keys('/home/userA/Desktop/chivalry-medieval-warfare.jpg')
或:
page.driver.browser.all(:xpath, '//input[@type="file"]').last.send_keys('/home/userA/Desktop/chivalry-medieval-warfare.jpg')
这篇关于水豚找不到文件输入字段来附加文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!