# Reading a line by line from local text file through WATIR

**URL:** <https://www.ruby-forum.com/t/reading-a-line-by-line-from-local-text-file-through-watir/73576>\
**Category:** Ruby\
**Created:** [October 25, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/reading-a-line-by-line-from-local-text-file-through-watir/73576 "2006-10-25T12:57:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![curious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6bbea6/32.png) [@curious](https://www.ruby-forum.com/u/curious)\
**Post date:** [October 25, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/reading-a-line-by-line-from-local-text-file-through-watir/73576/1 "2006-10-25T12:57:44Z")

</div>

I need to process a web page by insert the value in a local text file  
line by line each time.

## for example, in the code below:

require ‘watir’

1.upto(10) do |num|

ie = Watir::IE.start(“[http://www.test.php](http://www.test.php)”)

ie.text\_field(:name, “info”).set(“a\_line\_from\_C:\text.txt”)

ie.button(:value, “Submit”).click

## end

I need to make the WATIR code read each line in C:\text.txt file one by  
one 10 times. Could anyone tell me how I should adjust the code above  
so that this WATIR code read each line in C:\text.txt file one by one  
ten times, and process each time with the value in each line in the  
text file??

thanks.

---

<div class="post-metadata">

**Author:** ![curious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6bbea6/32.png) [@curious](https://www.ruby-forum.com/u/curious)\
**Post date:** [October 25, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/reading-a-line-by-line-from-local-text-file-through-watir/73576/2 "2006-10-25T15:33:57Z")

</div>

curious wrote:

> I need to make the WATIR code read each line in C:\text.txt file one by  
> one 10 times. Could anyone tell me how I should adjust the code above  
> so that this WATIR code read each line in C:\text.txt file one by one  
> ten times, and process each time with the value in each line in the  
> text file??

Watir has very little to with it, you’ve already got the code to  
populate the textbox and click the “Submit” button. The question is how  
do you open a file and read each line. The following should do it.

file = File.open(“c:/text.txt”, “r”)  
lines = file.readlines()

#-- make 10-passes thru this loop  
1.upto(10) {

#-- process each element in the array  
0.upto(lines.length - 1) {|j|  
ie.text\_field(:name, “info”).set(lines[j])  
ie.button(:value, “Submit”).click  
}

}

file.close()  
lines.clear()
