codemilan
codemilan

Reputation: 1082

Only one image getting uploaded multiple times

I have been using mechanize gem to scrape data from craigslist, I have a piece of code that uploads multiple image to craigslist, all the file paths are correct, but only single image gets uploaded multiple times what's the reason.

 unless pic_url_arry.blank?
        unless page.links_with(:text => 'Use classic image uploader').first.blank?
          page = page.links_with(:text => 'Use classic image uploader').first.click
        end
        puts "After classic image uploader"
        form = page.form_with(class: "add")
        # build full file path before setting like this => file = File.join( APP_ROOT, 'tmp', 'image.jpg')
        i = 0
        pic_url_arry = pic_url_arry.shuffle
        pic_url_arry.each do |p|
          form.file_uploads.first.file_name = p
          i+= 1
          page = form.submit
          puts "******#{p.inspect}*******"
          puts "******#{page.inspect}*******"
        end unless pic_url_arry.blank?
        # check if the file uploaded sucessfully with no. of files with no. of imgbox on page.
        check_image_uploaded = page.at('figure.imgbox').count
        if check_image_uploaded.to_i == i.to_i
          # upload failure craiglist or net error.
        end
      end  

AND the pic array has value as ["/home/codebajra/www/office/autocraig/public/uploads/posting_pic/pic/1/images__4_.jpg", "/home/codebajra/www/office/autocraig/public/uploads/posting_pic/pic/2/mona200.jpg", "/home/codebajra/www/office/autocraig/public/uploads/posting_pic/pic/3/images__1_.jpg"].

Upvotes: 1

Views: 104

Answers (1)

roarfromror
roarfromror

Reputation: 376

The form holding filefield is being set only once, which is taking only one image that hits first. So, the updated code will be,

unless pic_url_arry.blank?
    unless page.links_with(:text => 'Use classic image uploader').first.blank?
      page = page.links_with(:text => 'Use classic image uploader').first.click
    end
    puts "After classic image uploader"
    form = page.form_with(class: "add")
    # build full file path before setting like this => file = File.join( APP_ROOT, 'tmp', 'image.jpg')
    i = 0
    pic_url_arry = pic_url_arry.shuffle
    pic_url_arry.each do |p|
      form.file_uploads.first.file_name = p
      i+= 1
      page = form.submit
      form = page.form_with(class: "add")
      puts "******#{p.inspect}*******"
      puts "******#{page.inspect}*******"
    end unless pic_url_arry.blank?
    # check if the file uploaded sucessfully with no. of files with no. of imgbox on page.
    check_image_uploaded = page.at('figure.imgbox').count
    if check_image_uploaded.to_i == i.to_i
      # upload failure craiglist or net error.
    end
  end  

hoping this will solve the problem.

Upvotes: 1

Related Questions