# Watir with javascript?

**URL:** <https://www.ruby-forum.com/t/watir-with-javascript/112167>\
**Category:** Ruby\
**Created:** [August 16, 2007, 12:55am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167 "2007-08-16T00:55:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 16, 2007, 12:55am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/1 "2007-08-16T00:55:48Z")

</div>

I have been using the watir gem for a few days, and i have come across a  
problem with Javascript. I trying to do things like click on text  
attributes, ect. I saw something in the watir examples but I’m not sure  
it’s exactly what I’m looking for. Any know how to use those commands  
with javascript???

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 16, 2007, 5:22am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/2 "2007-08-16T05:22:56Z")

</div>

On Aug 15, 4:55 pm, Erik B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## I have been using the watir gem for a few days, and i have come across a problem with Javascript. I trying to do things like click on text attributes, ect. I saw something in the watir examples but I’m not sure it’s exactly what I’m looking for. Any know how to use those commands with javascript???
> 
> Posted viahttp://www.ruby-forum.com/.

you shoud post this to the watir group -  
[http://groups.google.com/group/watir-general?lnk=srg](http://groups.google.com/group/watir-general?lnk=srg)  
Also please show some html/code etc as thats far moremeaningful than  
saying you want to click on some text

Paul

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 16, 2007, 6:20am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/3 "2007-08-16T06:20:58Z")

</div>

> you shoud post this to the watir group -  
> [http://groups.google.com/group/watir-general?lnk=srg](http://groups.google.com/group/watir-general?lnk=srg)  
> Also please show some html/code etc as thats far moremeaningful than  
> saying you want to click on some text
> 
> Paul

Sorry for not being specific enough :(. Ok i’ll start off with what  
exactly i want to do, my friend playing a game called _runescape_ you  
may of heard of it? Anyways, i thought it would be cool if i could write  
him a program using watir that would go onto the site, and navigate  
though, and logg him in. _totally pointless, but doing little projects  
like this is the only way can really learn_  
Here’s there first link i want to click on:  
li class=“i-play”\>Play RuneScape (Existing  
user)

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 16, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/4 "2007-08-16T15:58:13Z")

</div>

You can pass click events with Watir pretty easily. As far as  
javascript is concerned, you _can_ directly manipulate it via watir,  
but usually, why bother?

Now, for more advanced questions, and future ones, yea ask on the  
watir mailing list.

Anyway, in this case, your problems aren’t with watir, they’re with a  
_VERY_ poorly constructed website. I just took a look at it, and I  
mean, yuck.  
Load it irb, and look at the page that way, everything is actually in a  
frame!  
$ie=Watir::IE.start(“[http://www.runescape.com](http://www.runescape.com)”)  
$ie.link(:text,/play runescape/i).click  
#oops, that _should_ work, but it doesn’t! Humm  
$ie.show\_frames  
there are 1 frames  
frame index: 1 name:  
#Ahh!  
$ie.frame(‘’).link(:text,/play runescape/i).click  
#yes. It’s that bad/poorly written. While there is a decent defense  
of using frames, not giving labels and ids to things is just bad.  
#ayway, that’s the first of two play runescape links, you can get to  
the second one this way  
#and then use any of the regular methods on it you wish, like click or  
flash or html or whatever.  
$ie.frame(‘’).link(:text=\>/play runescape/i,:index=\>2)

–Kyle

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 17, 2007, 6:02am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/5 "2007-08-17T06:02:45Z")

</div>

> frame!  
> $ie=Watir::IE.start(“[http://www.runescape.com](http://www.runescape.com)”)  
> $ie.link(:text,/play runescape/i).click  
> #oops, that _should_ work, but it doesn’t! Humm  
> $ie.show\_frames  
> there are 1 frames  
> frame index: 1 name:  
> #Ahh!  
> $ie.frame(‘’).link(:text,/play runescape/i).click  
> #yes. It’s that bad/poorly written. While there is a decent defense  
> of using frames, not giving labels and ids to things is just bad.  
> #ayway, that’s the first of two play runescape links, you can get to  
> the second one this way  
> #and then use any of the regular methods on it you wish, like click or  
> flash or html or whatever.  
> $ie.frame(‘’).link(:text=\>/play runescape/i,:index=\>2)
> 
> –Kyle

Ok… right now… i think I’m going insane or something???  
So at first i try  
require ‘watir’  
$ie=Watir::IE.start(“[http://www.runescape.com](http://www.runescape.com)”)  
$ie.link(:text,/play runescape/i).click  
works brilliantly,  
so i went farther into the site (choosing detail)  
so i add on  
require ‘watir’  
$ie=Watir::IE.start(“[http://www.runescape.com](http://www.runescape.com)”)  
$ie.link(:text,/play runescape/i).click  
ie.button(:value, “low detail”).click  
once again… great success \*i don’t think i added the $ infront of the  
ie.button?  
so i go to choose a world… doesn’t work… so i tried just choosing  
detail… nothing… i tried just doing what Kyle 1st said,  
require ‘watir’  
$ie=Watir::IE.start(“[http://www.runescape.com](http://www.runescape.com)”)  
$ie.link(:text,/play runescape/i).click…  
NOW NOTHING WORKS, i know I’m overreacting… but this just for some  
reason is really bugging me… heres the error i get.  
Microsoft Windows XP [Version 5.1.2600]  
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Schmode\>cd\

C:\>hell.rb  
./watir.rb:1928:in `assert_exists': Unable to locate object, using text and (?i- mx:play runescape (existing user)) (Watir::Exception::UnknownObjectException) from ./watir.rb:2009:in `click’  
from C:/hell.rb:3

C:\>  
Much thanks for anyone that helps 🙂

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 17, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/6 "2007-08-17T15:32:49Z")

</div>

First a bit of general ruby stuff:  
The $ infront of ie means it’s global.

That can be useful when writing little one off methods for your  
automation in irb, or when you’re writing a script.  
Does that help?

See,  
foo=:local  
$foo=:global  
@foo=:class  
@@foo=:shared\_between\_all\_instances\_of\_class  
These are _all_ different variables, they can each store a different  
value, and have their own rules about where/when you can access them.  
Try this  
foo=“local variable”  
$foo=“huge global variable”  
@foo=“class variable (but we can abuse it and treat it as global in  
small scripts)”

def up()  
$foo.upcase!  
@foo.upcase!  
end  
up()  
puts $foo  
puts @foo  
#now try  
def bad  
foo.upcase!  
end  
bad()  
#You get an error here, since foo is local, it doesn’t exist in the  
method!  
#if this doesn’t make much sense, try some of the 30 minute type ruby  
#tutorials. I think it’s hard to learn watir without a handle on the  
basics of ruby 🙂

Now, onto the rest of it.  
Humm, that showed up as a frame for me, but maybe they did some update?  
Now, inside of irb, you only require watir once, doing it more than  
once, won’t hurt, but it will cough a bit.

Watir provides some nice .exists? methods; very useful when prodding  
and poking a website with irb/watir.  
$ie.link(:text,/play runescape/i).exists?  
#That will tell you whether or not it exists!  
#A quick & dirty thing to see all links is  
$ie.links.each{|link| puts link}  
#but my guess is that its in another stupid frame so if the previous  
line  
#doesn’t have the link you want, try  
$ie.showFrames()

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [August 18, 2007, 1:29am UTC](https://www.ruby-forum.com/t/watir-with-javascript/112167/7 "2007-08-18T01:29:06Z")

</div>

Well, i fixed it i was being a complete tard, and typed in ie.link, not  
ie.frame. some how this got messed up from there? i don’tknow… maybe i  
was going insane _washing too many dishes @ my summer job??? 😛_  
Anyways… things are working awesome, took me a looong time to figure  
out that not all the things there are links, some are buttons, which  
meant that i had to use :value not :text…  
On a side not _and as an attempt not to spam the forums_ I’m reading  
this online book, Programming Ruby : the pragmatic program and i came  
across attributes.  
class Song  
def duration  
@duration  
end  
end  
aSong = Song.new(“Bicylops”, “Fleck”, 260)  
aSong.duration Â» 260  
Now to me theses look a lot like what my cousin was calling “keywords”  
like .zip and what not. are these the same thing but the keywords or  
_built in_ attributes?
