我希望将文件添加到<input type="file">
下面是html的一个片段。
<span class="btn btn-xs btn-primary btn-file"> #found
<span class="blahicon blahicon-upload"></span>
Browse
<input type="file" data-bind="value: fileName, event: { change: uploadImagesOnChange }"
accept="blah/txt" multiple=""> #not found
</span>这是水豚和红宝石
within_frame('frame1') do
within_frame('frame2') do
within(:xpath, [containing span xpath]) do # finds this
find(:xpath, './/*[@type="file"]').send_keys('C:\Users\...\blah.txt') #ElementNotFound
end
end
end我看不到隐藏的块,它是超作用域的。有什么想法吗?
发布于 2016-12-23 03:41:08
根据包装器类上的类btn-file判断,您可能正在使用Bootstrap和一种“标准”方法来隐藏实际的文件输入元素,以便它可以在多个浏览器上使用相同的样式。有许多方法可以隐藏按钮,从仅仅设置显示:没有在它上面,到更“现代”的方法,将它扩展到与替换按钮相同的大小,并将其不透明度设置为0,以便它成为替换按钮上的透明覆盖。
在Capybara中处理这种设置的基本策略是首先使用execute_script使元素可见,然后像往常一样使用attach_file或set。例如,如果您的站点使用隐藏文件元素的不透明方法,您可以这样做
within_frame('frame1') do
within_frame('frame2') do
within(:xpath, [containing span xpath]) do # finds this
file_input = find(:file, visible: :all)
page.driver.browser.execute_script("$(arguments[0]).css('opacity',1)", file_input.native)
file_input.set('C:\Users\...\blah.txt')
end
end
end注意--这段代码假设您在页面中使用的是jQuery,并且将只使用selenium驱动程序,因为它使用selenium驱动程序特定的功能在execute_script调用中将元素从水豚传递到selenium。如果不使用jQuery,则需要更改JS;如果使用其他驱动程序,则需要使用DOM方法在JS脚本中找到该元素,然后修改其不透明度。
https://stackoverflow.com/questions/41272540
复制相似问题