Denis S.
Denis S.

Reputation: 471

How to access hidden file upload field with Selenium WebDriver

I have such HTML:

enter image description here

upload_input = $driver.find_element(:id, "//input[@name = 'coupon_file']")
$driver.execute_script "$('input').show();"
upload_input.send_keys file

I'm trying to upload a file but get the error:

no such element: Unable to locate element

Upvotes: 0

Views: 249

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23815

upload_input = $driver.find_element(:id, "//input[@name = 'coupon_file']")

Actually you're doing incorrect. You're trying to locate upload element using xpath syntax but mentioned id locator which is incorrect. It should be as below :-

upload_input = $driver.find_element(:xpath, "//input[@name = 'coupon_file']")
upload_input.send_keys file

Or you can easily locate this upload element using name locator instead as :-

upload_input = $driver.find_element(:name, "coupon_file")
upload_input.send_keys file

Upvotes: 1

Related Questions