# Watir : Working in same session of IE

**URL:** <https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281>\
**Category:** Ruby\
**Created:** [April 30, 2008, 11:52am UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281 "2008-04-30T11:52:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [April 30, 2008, 11:52am UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281/1 "2008-04-30T11:52:14Z")

</div>

Hi,

Actually am stuck in a problem.

I am performing the following set of commands:

require ‘watir’

ie = Watir::IE.new  
ie.goto(“[http://www.google.com](http://www.google.com)”)

When I execute, it successfully opens a new browser with the correct  
google url.

Now, in this code, I simply want to add:

ie.text\_field(:name, “q”).set ‘roger federer’

If I append the above line to the above code, it will open a new session  
of IE and will perform the operation of setting the text box too, but my  
objective is not to open a new IE with google url but to perform the set  
command in the existing browser only.

Can anyone help how to do that?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [April 30, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281/2 "2008-04-30T12:03:51Z")

</div>

From: removed\_email\_address@domain.invalid

# Actually am stuck in a problem.

# I am performing the following set of commands:

# require ‘watir’

# ie = Watir::IE.new

# ie.goto(“[http://www.google.com](http://www.google.com)”)

# When I execute, it successfully opens a new browser with the correct

# google url.

# Now, in this code, I simply want to add:

# ie.text\_field(:name, “q”).set ‘roger federer’

# If I append the above line to the above code, it will open a

# new session of IE

that’s weird. i just tried your code, and it does \*not open another  
(new) session (in that case, i only have 1 ie session open, not 2).

kind regards -botp

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [April 30, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281/3 "2008-04-30T12:13:04Z")

</div>

PeÃ±a, Botp wrote:

> From: removed\_email\_address@domain.invalid
> 
> # Actually am stuck in a problem.
> 
> # I am performing the following set of commands:
> 
> # require ‘watir’
> 
> # ie = Watir::IE.new
> 
> # ie.goto(“[http://www.google.com](http://www.google.com)”)
> 
> # When I execute, it successfully opens a new browser with the correct
> 
> # google url.
> 
> # Now, in this code, I simply want to add:
> 
> # ie.text\_field(:name, “q”).set ‘roger federer’
> 
> # If I append the above line to the above code, it will open a
> 
> # new session of IE
> 
> that’s weird. i just tried your code, and it does \*not open another  
> (new) session (in that case, i only have 1 ie session open, not 2).
> 
> kind regards -botp

Hi botp,

Are you trying this out in Scite or is their something else that you are  
using?I believe when you execute (F5) the first two lines of code, it  
will open IE with google url.

Now, with the google url already open, I want to update the code in such  
a way that I can enter the text in the search field without opening a  
new session of the browser. So, if I just add the 3rd line of my code  
and execute, it would definitely open a new instance of IE (since it  
will execute ie = Watir::IE.new again). I want to avoid that, hoping  
that there must be a logic between the first 2 lines and the 3rd line  
which avoids opening a new browser.

Another idea would be to check if there is an existing instance of IE  
already present and then we can just do ie.goto(url).

Can you help with the logic?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [April 30, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281/4 "2008-04-30T14:25:41Z")

</div>

On 30 Apr, 10:52, Anukul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> objective is not to open a new IE with google url but to perform \> the set command in the existing browser only.  
> It might have been better to post this on the Watir list; you can  
> subscribe here;  
> [http://wtr.rubyforge.org/community.html](http://wtr.rubyforge.org/community.html)

I think Ruby linguistic answers would be best answered at ruby.lang.

If we look at the Watir API doc  
[http://wtr.rubyforge.org/rdoc/](http://wtr.rubyforge.org/rdoc/)

we note ie.attach

"Return a Watir::IE object for an existing IE window. Window can be  
referenced by url, title, or window handle. Second argument can be  
either a string or a regular expression in the case of of :url  
or :title. IE.attach(:url, ‘[www.google.com](http://www.google.com)’) IE.attach(:title,  
‘Google’) IE.attach(:hwnd, 528140) This method will not work when  
Watir/Ruby is run under a service (instead of a user). "

Regards

Aidy  
[www.agiletester.co.uk](http://www.agiletester.co.uk)

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [April 30, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/watir-working-in-same-session-of-ie/136281/5 "2008-04-30T12:31:58Z")

</div>

From: Anukul S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Are you trying this out in Scite or is their something else

# that you are

# using?I believe when you execute (F5) the first two lines of code, it

# will open IE with google url.

# Now, with the google url already open, I want to update the

# code in such

# a way that I can enter the text in the search field without opening a

# new session of the browser. So, if I just add the 3rd line of my code

# and execute, it would definitely open a new instance of IE (since it

# will execute ie = Watir::IE.new again). I want to avoid that, hoping

# that there must be a logic between the first 2 lines and the 3rd line

# which avoids opening a new browser.

nope. scite will reexecute your code fr start.

try irb, it will execute your code per line you enter.

# Another idea would be to check if there is an existing instance of IE

# already present and then we can just do ie.goto(url).

never tried that. that would mean searching for an ie instance and then  
attaching an ole object on it. but i do not know the criteria since  
there could be many ie instances open and many google pages open. wc one  
then? but that would be cool though.

kind regards -botp
