How do I attach a file when the input.file has a dynamic id?

左心房为你撑大大i 提交于 2020-01-15 04:28:43

问题


I have run into a snag with dynamic ids. The attach_file command needs the id name of the input type="file". The problem is that the id is dynamic

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

Is there a way to get the id of an element? something like...

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

回答1:


attach_file internally just passes filename to Capybara::Node::Element#set method.

So you can use:

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



回答2:


You can get the attribute of an element by doing:

element['attribute_name']

So for your example, to get the id attribute of the input with name 'file_upload', you can do:

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


来源:https://stackoverflow.com/questions/15165455/how-do-i-attach-a-file-when-the-input-file-has-a-dynamic-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!