Search code examples
rubywatircloud9

Can't upload file using Watir, Headless Browser, Chromedriver, using Ruby


I am trying to automate the creation of a listing on my website and I am having a lot of difficulty uploading an image for the listing. I am using Cloud9 and Watir, and using a headless chrome browser and the code is written in Ruby. For some reason the photo refuses to upload.

Click to see picture of upload area

photo = File.open("00909_8qYJaR8wTix_600x450.jpg", "a") 
path = File.expand_path(File.dirname(photo))
browser.file_field(:type,"file").set(path)

Output on terminal:

[8] pry(main)> browser.file_field(:type,"file").value
=> ""
[9] pry(main)> path
=> "/home/ubuntu/workspace/scraper"
[10] pry(main)> File.exist? (path)
=> true

HTML input class="fileupload" multiple="multiple" type="file" style="display: inline-block;"


Solution

  • Shouldn't path variable contain the path to actual file? From your example it looks like it's pointing to parent directory. Ditch the File.dirname(photo) turning it to this:

    photo = File.open("00909_8qYJaR8wTix_600x450.jpg", "a") 
    path = File.expand_path(photo)
    browser.file_field(:type,"file").set(path)
    

    and you should be fine, in my opinion.