Forum: Ruby ReferenceError: link is not defined (Selenium::WebDriver::Error::JavascriptError)

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 10:53
I tried this code :-

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Cascading_Style_Sheets"

link_arr = driver.find_elements(:xpath,"//a")[0..3]
link_arr.map!{|e| e.attribute("href")}
#puts link_arr

link_arr.each do |link|
   driver.execute_script("window.open(link)")
end


But getting error as -`assert_ok': ReferenceError: link is not defined
(Selenium::WebDriver::Error::JavascriptError)

I follwoed the link :- http://www.w3schools.com/jsref/met_win_open.asp

Can anyone help me ? why is it not working ?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 12:02
"window.open(link)" tells JavaScript to open a window and go to "link",
which is not a valid JavaScript variable.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 12:15
Joel Pearson wrote in post #1120740:
> "window.open(link)" tells JavaScript to open a window and go to "link",
> which is not a valid JavaScript variable.


but the "link" only the url string. If I try this way,it works.

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Cascading_Style_Sheets"

link_arr = driver.find_elements(:xpath,"//a")[0..3]
link_arr.map!{|e| e.attribute("href")}

# link_arr.each do |link|
   # p link
   # #driver.execute_script("window.open(link)")
# end
driver.execute_script('window.open("http://www.wikilovesmonuments.in/?pk_campaign=Cent...)
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 12:35
I recommend learning how this works:

http://en.wikibooks.org/wiki/Ruby_Programming/Synt...

driver.execute_script("window.open('#{link}')")
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 12:43
It is an interpolation issue. I figured it out :-

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Cascading_Style_Sheets"

link_arr = driver.find_elements(:xpath,"//a")[0..2]
link_arr.map!{|e| e.attribute("href")}

link_arr.each do |link|
     driver.execute_script("window.open(\"#{link}\")") if link
end
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 12:44
Joel Pearson wrote in post #1120742:
> I recommend learning how this works:
>
> http://en.wikibooks.org/wiki/Ruby_Programming/Synt...
>
> driver.execute_script("window.open('#{link}')")


Yes,I managed it by doing some debugging. Thanks for your help!
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 13:29
I generally avoid having to escape quote characters within JavaScript by
using %Q

%Q| "Some Text ' Containing " ' Quotes |
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 14:38
@Joel need one more help! Actually I am trying to open the the new pages
in a new tabs,instead of a new window. Accordingly I wrote this code :-

I took help from here - http://preferential.mozdev.org/preferences.html

require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.tabs.opentabfor.windowopen'] = true

driver = Selenium::WebDriver.for :firefox,:profile => profile
driver.navigate.to "http://en.wikipedia.org/wiki/Cascading_Style_Sheets"

link_arr = driver.find_elements(:xpath,"//a")[2..3]
link_arr.map!{|e| e.attribute("href")}

link_arr.each do |link|
     driver.execute_script("window.open(\"#{link}\",'_blank')") if link
end


But still they are being opened in a new window. How to resolve this ?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 14:47
Window vs Tab is a browser setting, not accessible by JavaScript as far
as I know. You need to change the way your browser handles new windows.

In Firefox: Tools -> Options -> Tabs -> Open new windows in a new tab
instead
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 14:53
Joel Pearson wrote in post #1120750:
> Window vs Tab is a browser setting, not accessible by JavaScript as far
> as I know. You need to change the way your browser handles new windows.
>
> In Firefox: Tools -> Options -> Tabs -> Open new windows in a new tab
> instead

All the stuffs I googled said if the above mentioned setting(as you have
shown) is checked,then `window.open(url,'_blank')` will open the url in
a new tab.So I did the below :-

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.tabs.opentabfor.windowopen'] = true

But not worked. :(
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 15:33
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 16:07
Joel Pearson wrote in post #1120756:
> Have a look at this:
> http://kb.mozillazine.org/Browser.link.open_newwin...


So what I am looking for is not possible right ? But after preference
setting,when I am checking to see if the preference is set or noe,it is
showing the check box of "Open new windows in a new tab
instead" as "unchecked" .

Please help I am confused. Earlier I did a different kind of stuffs
using the mozilla preference link,those worked.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 16:52
Having dug a little deeper, I found a post which stated that Selenium
Webdriver does not support opening links in a new tab:
http://code.google.com/p/selenium/issues/detail?id=3380

This make sense, as it doesn't support navigating between tabs, only
between windows. Otherwise you wouldn't be able to get back to the
previous tab, rendering the purpose of using tabs somewhat moot.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 17:08
Joel Pearson wrote in post #1120766:
> Having dug a little deeper, I found a post which stated that Selenium
> Webdriver does not support opening links in a new tab:
> http://code.google.com/p/selenium/issues/detail?id=3380
>
> This make sense, as it doesn't support navigating between tabs, only
> between windows. Otherwise you wouldn't be able to get back to the
> previous tab, rendering the purpose of using tabs somewhat moot.


Really make sense!!
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 17:43
Love U Ruby wrote in post #1120768:
> Really make sense!!

Well, "make sense" works phonetically, since the "s" at the beginning of
"sense" acts the same as a trailing "s" from "makes", but I did spell
that incorrectly.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-05 18:05
Joel Pearson wrote in post #1120772:
> Love U Ruby wrote in post #1120768:
>> Really make sense!!
>
> Well, "make sense" works phonetically, since the "s" at the beginning of
> "sense" acts the same as a trailing "s" from "makes", but I did spell
> that incorrectly.

Sorry for that!! Thanks for correcting! :)
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-08 13:46
Joel Pearson wrote in post #1120772:
> Love U Ruby wrote in post #1120768:
>> Really make sense!!

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
url = 'https://www.google.co.in/'
t = Thread.new(url,driver) do |u,d|
  d.get("https://www.google.co.in")
  d.execute_script("alert('I AM HERE');")
end

t.join()

getting error as:

[remote server]
file:///tmp/webdriver-profile20130908-4868-dn1alc/extensions/fxdriver@googlecode.com/components/driver_component.js:8360:in
`r': waiting for evaluate.js load failed
(Selenium::WebDriver::Error::JavascriptError)
  from [remote server]
file:///tmp/webdriver-profile20130908-4868-dn1alc/extensions/fxdriver@googlecode.com/components/driver_component.js:392:in
`fxdriver.Timer.prototype.runWhenTrue/g'
  from [remote server]
file:///tmp/webdriver-profile20130908-4868-dn1alc/extensions/fxdriver@googlecode.com/components/driver_component.js:386:in
`fxdriver.Timer.prototype.setTimeout/<.notify'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in
`assert_ok'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:in
`initialize'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in
`new'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in
`create_response'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in
`request'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in
`call'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in
`raw_execute'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in
`execute'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:328:in
`executeScript'
  from
/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/driver.rb:213:in
`execute_script'
  from /home/kirti/ruby/SO.rb:7:in `block in <main>'
[Finished in 62.6s with exit code 1]

What wrong I did ?
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-09-08 14:27
Love U Ruby wrote in post #1120944:

> require "selenium-webdriver"
>
> driver = Selenium::WebDriver.for :firefox
> url = 'https://www.google.co.in/'
> t = Thread.new(url,driver) do |u,d|
>   d.get("https://www.google.co.in")
>   d.execute_script("alert('I AM HERE');")
> end
>
> t.join()
>
> getting error as:
>
> [remote server]

file:///tmp/webdriver-profile20130908-4868-dn1alc/extensions/fxdriver@googlecode.com/components/driver_component.js:8360:in
> `r': waiting for evaluate.js load failed
> (Selenium::WebDriver::Error::JavascriptError)

`gem update selenium-webdriver` solved the issue.. :)
8bdc88dbbc0d35284cfed0982f0172a8?d=identicon&s=25 sudheer babu (Guest)
on 2013-10-18 14:00
(Received via mailing list)
Thanks for the wonderful information and it is useful forselenium
Learners.123trainings provides best <a
href="http://123trainings.com/it-selenium-online-trainin...
SELENIUM training</a>.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.