当input.file具有动态ID时,如何附加文件?

Tan*_*eam 2 ruby file-upload cucumber capybara

我遇到了动态ID问题.attach_file命令需要输入type ="file"的id名称.问题是id是动态的

(id="document_22") #indicating the 22nd document uploaded to this section.
Run Code Online (Sandbox Code Playgroud)

有没有办法获得元素的id?就像是...

attach_file(find(:xpath, ".//input[@name='file_upload']").get('@id'),
'C:\\Users\\testbox\\Documents\\testdoc.xls')
Run Code Online (Sandbox Code Playgroud)

And*_*lov 8

attach_file内部只是将filename传递给Capybara::Node::Element#set方法.

所以你可以使用:

find(:xpath, ".//input[@name='file_upload']").set(filename)
Run Code Online (Sandbox Code Playgroud)