我使用红宝石,黄瓜和水豚。
我的html看起来像这样:
<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>
基本上,我要做的是通过单击链接“上传”来上传文件:
<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>
但是我一直失败,因为水豚无法找到必要的元素...我已经尝试了很多东西:
attach_file('//*[@id="promocodesUpload"]/div/a/input', File.absolute_path($campaign_promotional_code_path))
结果:找不到文件字段“ // * [@ 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)
和
attach_file("Upload", File.absolute_path($campaign_promotional_code_path)
结果:找不到文件字段“上传”(Capybara :: ElementNotFound)
和
click_link("Upload")
结果:找不到链接“上传”(Capybara :: ElementNotFound)
和
有帮助吗? :)
Capybara找不到文件输入,因为它的不透明度为0,所以它被隐藏在页面上。 -最好的选择是使用execute_script修改文件输入的样式属性,以便在调用attach_file之前可以看到它]
如果您的网页上有jQuery:
尝试一下:
根据documentation,如果元素以某种方式隐藏,则可以将make_visible
选项设置为true。