# Click a javascript dialog window in Firefox

**URL:** <https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195>\
**Category:** ruby-talk\
**Created:** [14 July 2010 16:39 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195 "2010-07-14T16:39:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Ruiz1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mario\_Ruiz1](https://rubytalk.org/u/Mario_Ruiz1)\
**Post date:** [14 July 2010 16:39 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/1 "2010-07-14T16:39:23Z")

</div>

Hi,  
I'm trying to click a dialog box in Firefox but all the code I found is  
not working.  
I have watir and firewatir 1.6.5

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [20 July 2010 11:29 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/2 "2010-07-20T11:29:14Z")

</div>

Firewatir::Firefox has a method startClicker that does that:

$ ri startClicker

---------------------------------------- FireWatir::Firefox#startClicker  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startClicker(button, waitTime = 1, userInput = nil, text = nil)

> **···**
>
> On Wed, Jul 14, 2010 at 6:39 PM, Mario Ruiz \<tcblues@gmail.com\> wrote:
> 
> > Hi,  
> > I'm trying to click a dialog box in Firefox but all the code I found is  
> > not working.  
> > I have watir and firewatir 1.6.5
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Description:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tells FireWatir to click javascript button in case one comes  
> after performing some action on an element. Matches  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text of pop up with one if supplied as parameter. If text  
> matches clicks the button else stop script execution until  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop up is dismissed by manual intervention.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Input:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button - JavaScript button to be clicked. Values can be OK or Cancel  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waitTime - Time to wait for pop up to come. Not used just  
> for compatibility with Watir.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userInput - Not used just for compatibility with Watir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text - Text that should appear on pop up.
> 
> What this really does underneath is to change the window.alert method  
> for one that returns what you tell it to. It doesn't really open a  
> window anymore.
> 
> I've used this successfully once.
> 
> Hope this helps,
> 
> Jesus.

---

<div class="post-metadata">

**Author:** ![Mario\_Ruiz1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mario\_Ruiz1](https://rubytalk.org/u/Mario_Ruiz1)\
**Post date:** [13 August 2010 11:53 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/3 "2010-08-13T11:53:25Z")

</div>

But the thing is... if you click first.. ff keeps waiting and doesn't  
return the control to ruby so in theory the only way to do it is call  
startClicker first and then click the button... but it's not working  
properly since what it is doing is just closing the JS window but not  
clicking on the button as you can see in this example:

require "firewatir"  
$ff = FireWatir::Firefox.new() #create an object to drive the browser  
$ff.goto("[http://w3schools.com/js/tryit\_view.asp?filename=tryjs\_confirm](http://w3schools.com/js/tryit_view.asp?filename=tryjs_confirm)")  
$ff.startClicker( "OK", 4)  
$ff.button(:value,"Show a confirm box").click  
sleep 3  
$ff.close()

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [13 August 2010 12:02 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/4 "2010-08-13T12:02:36Z")

</div>

I don't know if you have other example that does something in the page  
when OK is clicked.  
What startClicker does, is to override the window.alert function with  
a function that returns OK, so really no popup is open at all.

Jesus.

> **···**
>
> On Fri, Aug 13, 2010 at 1:53 PM, Mario Ruiz \<tcblues@gmail.com\> wrote:
> 
> > But the thing is... if you click first.. ff keeps waiting and doesn't  
> > return the control to ruby so in theory the only way to do it is call  
> > startClicker first and then click the button... but it's not working  
> > properly since what it is doing is just closing the JS window but not  
> > clicking on the button as you can see in this example:
> > 
> > require "firewatir"  
> > $ff = FireWatir::Firefox.new() #create an object to drive the browser  
> > $ff.goto("[http://w3schools.com/js/tryit\_view.asp?filename=tryjs\_confirm&quot;\](http://w3schools.com/js/tryit_view.asp?filename=tryjs_confirm&quot;%5C))  
> > $ff.startClicker( "OK", 4)  
> > $ff.button(:value,"Show a confirm box").click  
> > sleep 3  
> > $ff.close()

---

<div class="post-metadata">

**Author:** ![Mario\_Ruiz1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mario\_Ruiz1](https://rubytalk.org/u/Mario_Ruiz1)\
**Post date:** [13 August 2010 12:07 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/5 "2010-08-13T12:07:32Z")

</div>

So there is no way to click the buttons on a javascript window

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [13 August 2010 12:08 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/6 "2010-08-13T12:08:48Z")

</div>

It also does the same with the confirm button, so your example should  
work. I've tested it and it works (debugging with Firebug I see that  
the call r = confirm.. returns true).

Jesus.

> **···**
>
> 2010/8/13 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> 
> > On Fri, Aug 13, 2010 at 1:53 PM, Mario Ruiz \<tcblues@gmail.com\> wrote:
> > 
> > > But the thing is... if you click first.. ff keeps waiting and doesn't  
> > > return the control to ruby so in theory the only way to do it is call  
> > > startClicker first and then click the button... but it's not working  
> > > properly since what it is doing is just closing the JS window but not  
> > > clicking on the button as you can see in this example:
> > > 
> > > require "firewatir"  
> > > $ff = FireWatir::Firefox.new() #create an object to drive the browser  
> > > $ff.goto("[http://w3schools.com/js/tryit\_view.asp?filename=tryjs\_confirm&quot;\](http://w3schools.com/js/tryit_view.asp?filename=tryjs_confirm&quot;%5C))  
> > > $ff.startClicker( "OK", 4)  
> > > $ff.button(:value,"Show a confirm box").click  
> > > sleep 3  
> > > $ff.close()
> > 
> > I don't know if you have other example that does something in the page  
> > when OK is clicked.  
> > What startClicker does, is to override the window.alert function with  
> > a function that returns OK, so really no popup is open at all.

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [13 August 2010 12:10 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/7 "2010-08-13T12:10:27Z")

</div>

Inside Firewatir no, but why do you really need that? Your javascript  
code will work as if the user had pressed the OK or the cancel button,  
so everything should continue executing normally.

Jesus.

> **···**
>
> On Fri, Aug 13, 2010 at 2:07 PM, Mario Ruiz \<tcblues@gmail.com\> wrote:
> 
> > So there is no way to click the buttons on a javascript window

---

<div class="post-metadata">

**Author:** ![Mario\_Ruiz1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mario\_Ruiz1](https://rubytalk.org/u/Mario_Ruiz1)\
**Post date:** [13 August 2010 12:14 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/8 "2010-08-13T12:14:36Z")

</div>

Because, in some cases the page is doing different things depending what  
I'm selecting... for example, the window says: do you want to go to  
[www.google.com](http://www.google.com)? and the options: Yes, No

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [13 August 2010 13:04 UTC](https://rubytalk.org/t/click-a-javascript-dialog-window-in-firefox/59195/9 "2010-08-13T13:04:45Z")

</div>

But then you can automate to one option or the other with  
startClicker("OK") or startClicker("cancel") (or maybe "Cancel", I'm  
not sure)

Jesus.

> **···**
>
> On Fri, Aug 13, 2010 at 2:14 PM, Mario Ruiz \<tcblues@gmail.com\> wrote:
> 
> > Because, in some cases the page is doing different things depending what  
> > I'm selecting... for example, the window says: do you want to go to  
> > [www.google.com](http://www.google.com)? and the options: Yes, No
