# Downloading a CSV using URI - mechanize

**URL:** <https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750>\
**Category:** Ruby\
**Created:** [May 6, 2013, 8:17pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750 "2013-05-06T20:17:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 6, 2013, 8:17pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/1 "2013-05-06T20:17:20Z")

</div>

Hi All,

## Got an issue. I am downloading a csv in a script written in Mechanize. I am setting some parameters on the webpage and then submit my form. This directs me to another page where i have the following href link (got it through firebug):

## CSV

## In my script to simulate click on this CSV href link, i take out the URI and download the file. The code for this is below:

## #Submitting the page with button Chnange Columns page=pageAgent.submit(checkboxForm,checkboxForm.buttons.first) #Retrieving the csv link href csv\_href=pageAgent.page.link\_with(:text =\> ‘CSV’).uri pageAgent.download("#{csv\_href}",‘c:/magma/1.csv’)}

Issue: The downloaded CSV file is missing some data which should be  
there. When i download the file directly from webpage i get correct  
results in csv file. So i was wondering, could it be an issue that i  
download the file using URI? As i noticed that the URI is lot bigger as  
compared to the href link generated on the page after the search  
parameters are set. Also if someone can tell me how can i download a  
file using the href itself using mechanize.

Help much appreciated.

I have also attached a txt file that has the URI and the href both for  
reference. Didnt wanted this post to be long to read unnecessarily. 🙂

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 6, 2013, 11:57pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/2 "2013-05-06T23:57:27Z")

</div>

If you want fresh data each time you need to extract the URI from the  
page every time.

Odds are using an older URI is giving you an older report.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 3:13am UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/3 "2013-05-07T03:13:52Z")

</div>

On Mon, May 6, 2013 at 1:17 PM, Rochit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Got an issue. I am downloading a csv in a script written in Mechanize. I  
> am setting some parameters on the webpage and then submit my form. This  
> directs me to another page where i have the following href link (got it  
> through firebug):

Show us the code. The two items you show in the attachment don’t quite  
match.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 6:56am UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/4 "2013-05-07T06:56:37Z")

</div>

tamouse mailing lists wrote in post #1108020:

> On Mon, May 6, 2013 at 1:17 PM, Rochit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Got an issue. I am downloading a csv in a script written in Mechanize. I  
> > am setting some parameters on the webpage and then submit my form. This  
> > directs me to another page where i have the following href link (got it  
> > through firebug):
> 
> Show us the code. The two items you show in the attachment don’t quite  
> match.

Hi Tamouse and Joel

I have attached the full script for your reference.

Thanks

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 10:40am UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/5 "2013-05-07T10:40:38Z")

</div>

Joel P. wrote in post #1108049:

> Obviously I can’t test your code under the same conditions, but from the  
> look of it you should get the same file if you do this manually.
> 
> The main thing which looks like it could go wrong is the checkbox  
> section.
> 
> allChkBoxes = checkboxForm.checkboxes  
> chkNames = # \<= This is overwritten as a String each time  
> allChkBoxes.each do |chkBx|  
> chkNames = chkBx.name # Why not just do “chkBx.check” ?  
> checkboxForm.checkbox\_with(:name =\> “#{chkNames}”).check  
> end
> 
> I think you’re doing something differently with the options when running  
> this manually. Unless there’s more than one link called CSV on the page?

Hi Joel, i am setting all the checkboxes to true, so when the csv is  
downloaded i get all the columns. Setting the checkbox as true adds that  
specific column in the csv. It should not affect the csv output.

What do you think? is my approach correct where i am getting the URI of  
the href ‘csv’ and dowloading the file. Also i think there is just one  
csv hyperlink on the page, though i will double check again.

thanks.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 11:04am UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/6 "2013-05-07T11:04:58Z")

</div>

As far as I can see, you are getting the correct CSV link, although I  
have to read between the lines as I can’t see the actual page or  
results.

I think what you need to do is isolate exactly what the difference is  
between the report you want and the one you’re getting. That should give  
you a bit more of a clue as to where the difference is coming from.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 10:14am UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/7 "2013-05-07T10:14:55Z")

</div>

Obviously I can’t test your code under the same conditions, but from the  
look of it you should get the same file if you do this manually.

The main thing which looks like it could go wrong is the checkbox  
section.

allChkBoxes = checkboxForm.checkboxes  
chkNames = [] # \<= This is overwritten as a String each time  
allChkBoxes.each do |chkBx|  
chkNames = chkBx.name # Why not just do “chkBx.check” ?  
checkboxForm.checkbox\_with(:name =\> “#{chkNames}”).check  
end

I think you’re doing something differently with the options when running  
this manually. Unless there’s more than one link called CSV on the page?

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 2:50pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/8 "2013-05-07T14:50:00Z")

</div>

Indeed. If there’s any AJAX going on; you’ll do better with  
watir-webdriver.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 9:37pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/9 "2013-05-07T21:37:11Z")

</div>

Joel P. wrote in post #1108083:

> Indeed. If there’s any AJAX going on; you’ll do better with  
> watir-webdriver.

Hi Joel, all.  
I think there is some issue when i set the multiselect  
listbox.select\_all.  
Somehow the options in the select list that have a space in them. Eg,  
‘Dev Task’ will not be selected. If option was ‘DevTask’ then it works  
fine.  
Could this be an issue with mechanize? Is there a way i can use regex  
while  
Selecting the options with a space. ?  
Thanks

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 2:31pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/10 "2013-05-07T14:31:02Z")

</div>

On Mon, May 6, 2013 at 11:56 PM, Rochit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Tamouse and Joel
> 
> I have attached the full script for your reference.
> 
> Thanks

Nothing pops out at me from a short inspection. What I would suggest  
is loading up the ruby debugger and stepping through it, inspecting  
your page and fragment values to see if you have indeed got what you  
think you should get. If the page you’re hitting on does any  
javascript to put in page content that is necessary to the execution  
of the function you want, mechanize won’t handle that.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 7, 2013, 11:39pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/11 "2013-05-07T23:39:47Z")

</div>

Joel P. wrote in post #1108049:

> chkNames = chkBx.name # Why not just do “chkBx.check” ?  
> checkboxForm.checkbox\_with(:name =\> “#{chkNames}”).check

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 8, 2013, 4:36pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/12 "2013-05-08T16:36:37Z")

</div>

Joel P. wrote in post #1108143:

> Joel P. wrote in post #1108049:
> 
> > chkNames = chkBx.name # Why not just do “chkBx.check” ?  
> > checkboxForm.checkbox\_with(:name =\> “#{chkNames}”).check

## Hi Joel, The above you mentioned is for the checkboxes. The issue is with this:

## searchForm.field\_with(:name =\> ‘bug\_severity’).select\_all

When i do select\_all for this MultiSelect called ‘bug\_severity’ it does  
not Select the options that have a space in them - eg, ‘Dev Task’ will  
not be selected.

Any ideas why and how to rectify?

Thanks

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [May 8, 2013, 5:00pm UTC](https://www.ruby-forum.com/t/downloading-a-csv-using-uri-mechanize/228750/13 "2013-05-08T17:00:02Z")

</div>

I’ve never used a multiselect in mechanize, so I don’t know about that.

I think you can usually select either on value or on content, perhaps  
the values don’t have spaces and you could use them.

Usually the best option is to work with the objects, so if you can find  
a way to iterate through each option and select it without having to  
access it by name, that might work.

Have a look at your options in IRB, something like:  
p searchForm.field\_with(:name =\> ‘bug\_severity’).methods.sort
