Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 337
TangibleDream Avatar asked Nov 30 '25 16:11

TangibleDream


2 Answers

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

So you can use:

find(:xpath, ".//input[@name='file_upload']").set(filename)
like image 70
Andrei Botalov Avatar answered Dec 02 '25 07:12

Andrei Botalov


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"
like image 40
Justin Ko Avatar answered Dec 02 '25 06:12

Justin Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!