# Question on watir

**URL:** <https://www.ruby-forum.com/t/question-on-watir/224437>\
**Category:** Ruby\
**Created:** [November 12, 2012, 5:45am UTC](https://www.ruby-forum.com/t/question-on-watir/224437 "2012-11-12T05:45:39Z")\
**Posts on this page:** 20\
**Page:** 7

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 11:16am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/121 "2013-03-20T11:16:18Z")

</div>

hi,

how can i make my program to wait until it chooses an element from the  
combo box? It’s choosing an element from the combo box.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 11:40am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/122 "2013-03-20T11:40:22Z")

</div>

Do you mean selecting an option within the element’s list, or do you  
mean selecting the element itself?  
It should wait anyway while selecting something. What is it doing  
instead?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 1:42pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/123 "2013-03-20T13:42:31Z")

</div>

yeah yeah it is waiting, I did some mistake here. Just I realized.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 19, 2013, 6:01am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/124 "2013-03-19T06:01:38Z")

</div>

hi, i used this code to handle this pop up.

def modalDialog(flg)  
begin  
#Watir::wait($browser.modal\_dialog().enabled)  
if(flg ==1)  
puts “modalDialog called”  
errLog=$browser.modal\_dialog().td(:class,“PopupMsgTitleMenu”).text  
$browser.modal\_dialog().table(:id,“OK”).click

```
else
  $browser.modal_dialog().table(:id,"OK").click
  errLog =""
end

return errLog

```

rescue Exception =\> e  
$logger.error " modalDialog - #{e.message}"  
end  
end

Thank you

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 3:20pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/125 "2013-03-20T15:20:45Z")

</div>

hi,

My browser is loading very slowly, due to that I am having this error,

C:/Ruby187/lib/ruby/gems/1.8/gems/watir-classic-3.2.0/lib/watir-classic/ie-class.rb:606:in  
`attach\_browser\_window’: Unable to locate a window with url of  
[http://localhost/login.do](http://localhost/login.do)  
(Watir::Exception::NoMatchingWindowFoundException)

how could I rectify this?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 5:25pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/126 "2013-03-20T17:25:32Z")

</div>

Sorry, these are both outside my area of expertise. Maybe some other  
kind soul will be able to help 🙂

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 21, 2013, 5:31am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/127 "2013-03-21T05:31:01Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 20, 2013, 3:42pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/128 "2013-03-20T15:42:59Z")

</div>

hi

I have another question, Server is overloaded when I automated this web  
page application, if i look into the server, many thread hasn’t been  
closed and remains idle,consequently,server gets overloaded very soon,  
Can i make sure that whether all thread are closed once I run the  
program. And I haven’t created any Thread program,but it still there is  
a message in server says many thread remains idle. How can i make sure  
threads are closed.?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 8:18am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/129 "2013-03-22T08:18:49Z")

</div>

hi,

puts $browser.windows.count  
$browser.windows.each do |w|  
puts w.title  
w.close  
end

The above code of yours prints the title but it’s not closing the  
window. You asked me to check whether it is a Javascript window,I  
checked that,it is not JavaScript alert,I have used your code of below

puts $browser.alert.text  
$browser.alert.close

It’s not working since it’s not JavaScript window. I told you I started  
to use the Model dialog box, But somehow it’s having some problem  
thereby I couldn’t continue with that. So can you give me the solution  
why it’s not closing the window, and also you have written the code of  
below

$browser.windows.last.use do  
$browser. (code to read the message here)  
end

Do i need to use like

$browser.windows.last.use do  
$browser.window.text  
end

to print the string in the pop up?And the pop up consist of OK and  
cancel button, now i need to have a way to press the both the buttons  
according to my requirement. Can you give me any solution to resolve  
this problem?

And also I can’t use the developer tool to view the elements in the pop  
up window, is there any way, can i know the id of the elements in the  
pop up window?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 11:04am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/130 "2013-03-22T11:04:16Z")

</div>

Ok,

how can I use choose the last window

for an instance you have given this code

“$browser.windows.last.use do”

Now how can i use this code to close the window?

I used like $browser.windows.last.close. But it is not working.  
RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 11:11am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/131 "2013-03-22T11:11:01Z")

</div>

Does it work with just a blank new window?

irb(main):106:0\> require ‘watir-webdriver’  
=\> true  
irb(main):107:0\> b = Watir::Browser.new  
=\> #\<Watir::Browser:0x…fcee16076 url=“about:blank” title=""\>  
irb(main):108:0\> b.windows.last.close  
=\> #\<Watir::Window:0x…fad045588 located=true\>

The above worked perfectly for me.

If that doesn’t work for you, what gem version & browser version are you  
working with?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 12:49pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/132 "2013-03-22T12:49:23Z")

</div>

hi,

I tried in the way you tried (i.e)through irb, I met up with pop  
problem, and then I open many browser and then I tried to close using  
this comment  
b.windows.last.close,all other windows are closed expect the one which  
consist of pop up. What could be the reason?

And I executed your above code, when I use watir-webdriver it opens the  
Firebox and when i use watir it opens the internet explorer, and it’s  
closing also. But it is not closing when the window is having a pop up.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 9:50am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/133 "2013-03-22T09:50:27Z")

</div>

Works for me:

* * *

irb(main):001:0\> require ‘watir-webdriver’  
=\> true  
irb(main):002:0\> b = Watir::Browser.new  
=\> #\<Watir::Browser:0x…fb514d2a0 url=“about:blank” title=“”\>  
irb(main):003:0\> b.goto ‘[google.com](http://google.com)’  
=\> “[http://www.google.co.uk/](http://www.google.co.uk/)”  
#Here I manually opened a new window from that session.  
irb(main):004:0\> b.windows.count  
=\> 2  
irb(main):005:0\> b.windows.each do |w|  
irb(main):006:1\* puts w.title  
irb(main):007:1\> end  
Google  
Gmail: Email from Google  
=\> [#\<Watir:🪟0x…fb49b4dbe located=true\>,  
#\<Watir:🪟0x…fd178cb4a located=true\>]  
irb(main):008:0\> b.windows.each do |w|  
irb(main):009:1\* w.close  
irb(main):010:1\> end  
=\> [#\<Watir:🪟0x…fb49b4dbe located=true\>,  
#\<Watir:🪟0x…fd178cb4a l  
ocated=true\>]  
#All Windows closed!

* * *

Perhaps there’s some Javascript code preventing the window from closing  
until you’ve made a selection. In which case you’ll need to interact  
with the window before closing it. The same principle applies, you just  
transfer control to the popup until you’re done with it.

When you use this:

$browser.windows.last.use do

The variable “$browser” will refer to the last window (or whichever one  
you choose in that line) until it reaches “end”. It should just be a  
matter of interacting with the selected window in the same way as you  
normally would.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 1:24pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/134 "2013-03-22T13:24:25Z")

</div>

I am using IE so i will try to reattach and also Can I read the message  
from the pop up?if so, what code do I need to return?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 1:16pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/135 "2013-03-22T13:16:38Z")

</div>

Raj pal wrote in post #1102781:

> all other windows are closed expect the one which  
> consist of pop up.
> 
> it is not closing when the window is having a pop up.

Then it’s something to do with the way the popup is created. I’d guess  
that the popup doesn’t have watir attached to it.

If you don’t have watir’s control of the popup, then you have a few  
options.

For IE you can use the COM object, or just use Watir to re-attach to it.

For Firefox you can either target the window, using the various ways of  
dealing with a window regardless of what it contains; or you could use  
the process library to kill the underlying process once you’ve finished  
with the browser, thereby terminating all windows.  
These approaches would also work with IE, but they should be second  
choice to using its COM object.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 1:57pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/136 "2013-03-22T13:57:27Z")

</div>

But how can I attach the pop ? why i am asking is, when I attach the  
window I use this code

b= Watir::IE.attach(:url,“[www.google.com](http://www.google.com)”)

But in the case of pop up I am not aware of url, If so, how would I  
attach the pop up?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 4:09pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/137 "2013-03-22T16:09:12Z")

</div>

In the version of Watir I have, you can just keep iterating via index.  
Something like this:

100.times { |t|  
puts ( Watir::Browser.attach( :index, t ) ).title rescue break  
}

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 1:55pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/138 "2013-03-22T13:55:23Z")

</div>

The way you read the message depends on exactly what the popup contains.  
Once you attach to it it’s just the same as a normal window, so treat it  
as such.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 4:59pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/139 "2013-03-22T16:59:05Z")

</div>

hi, I am using that model dialog box again where again I am having this  
attaching problem, I have written your code of below, It’s perfectly  
working, thank you very much, But I couldn’t able to understand how this  
code is working, each and every time you are passing value through 1 to  
100. But what is exactly happening when it encounters  
Watir::Browser.attach(:index, 1) or Watir::Browser.attach( :index, 2 ) )  
? could you explain me?

100.times { |t|  
puts ( Watir::Browser.attach( :index, t ) ).title rescue break  
}

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 22, 2013, 5:11pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/140 "2013-03-22T17:11:57Z")

</div>

This is how it works if you have 2 IE windows open:

* * *

do…

Watir::Browser.attach( :index, 0 ) #Grab the first window

puts .title #Output the window title

loop #Onward to index 1!

Watir::Browser.attach( :index, 1 ) #Grab the second window

puts .title #Output the window title

loop #Sally forth to index 2!

Watir::Browser.attach( :index, 2 ) #Oh no, there’s no 3rd window!

rescue #Catch the error caused by not finding a window

break #Exit the loop

end

* * *

And that’s how it works.  
It’s not an ideal solution as you will get a delay before timing out on  
grabbing the non-existent upper indexed window, but given that you don’t  
know how many windows are open, that’s the best I could come up with on  
short notice.

[Previous page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=6)

[Next page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=8)
