0

I am trying to automatically select a file into an upload prompt. Here is the code for the upload section of the website.

<input name="__RequestVerificationToken" value="rAyNscZYeYLEYTV1rodwr0lQr_0Eadpfd11lcuHUvJf83lM57R2vruocFLDPjK0axYr_XBYgqmgTXVH_V2qHGFpAq-zGx_mMm72XIUj4Z6HlUjcyz47Vepfjysur7CR0N8xgHMkjW1KRgb4K6w6VPQ2" type="hidden">
<li>
  <input id="imageUpload" class="hideupload" name="imageUpload" multiple="" type="file">
  <a id="imageUploadLink" href="#"><i class="addContent_icon uploadlink"><img src="/Content/images/addContent-icon-images.png"></i>Upload Images</a>
</li>

The element of interest as far as I can tell is id: imageUploadLink. Clicking that will open the prompt to select file. Here is my code.

require "selenium-webdriver"
browser = Selenium::WebDriver.for :firefox
sign_in(browser, myUsername, myPassword) #signs into testing site
browser.find_element(:id, 'imageUploadLink').click
element = browser.find_element(:id, 'imageUploadLink')
element.send_keys "/Users/DanielScarlett/Desktop/Example.JPG"

send_keys seems to be typed into the bottom of the browser in a sort of find section that disappears quickly: https://i.stack.imgur.com/y6QWr.jpg

I have tried many different ways to implement this, and nothing seems to work. Also, couldn't find anything anywhere to handle this.

MKay
  • 818
  • 9
  • 32

2 Answers2

0

I assume you want to select file via browse and then click on Upload Images link. If that is the case, ID for your interest is imageUpload. Code is in Java, you can change for ruby.

It worked for me using :

driver.findElement(By.id("imageUpload")).sendKeys("absolutepathtoimage\\testImage.jpg");

Check out the use of not using click() on button. It triggers the OS level modal dialog where selenium fails to automate. I referred it from How to upload file using Selenium WebDriver in Java answer given by @talktokets

Community
  • 1
  • 1
MKay
  • 818
  • 9
  • 32
0

When uploading files with Selenium you want to add the file path to the form (e.g., id: 'imageUpload') and then submit the form. You want to avoid triggering the system level dialog box since Selenium isn't able to handle this.

For Ruby, it would look like this:

file_upload = driver.find_element(id: 'imageUpload')
file_upload.send_keys('path/to/your/file')
file_upload.submit

You can see a full write-up that steps through this here.

And if you're looking to run your tests on a remote node (e.g., with Selenium Grid or on a third-party like Sauce Labs) then you'll want to take a look at the file_detector method.

tourdedave
  • 11
  • 1