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

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

(id="document_22") #indicating the 22nd document uploaded to this section. 

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

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

attach_file内部只是将文件名传递给Capybara Capybara::Node::Element#set #set方法。

所以你可以使用:

 find(:xpath, ".//input[@name='file_upload']").set(filename) 

您可以通过执行以下操作获取元素的属性:

 element['attribute_name'] 

因此,对于您的示例,要获取名为“file_upload”的输入的id属性,您可以执行以下操作:

 find(:xpath, ".//input[@name='file_upload']")['id'] #=> "document_22"