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

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 ?

···

--
Posted via http://www.ruby-forum.com/.

"window.open(link)" tells JavaScript to open a window and go to "link",
which is not a valid JavaScript variable.

···

--
Posted via http://www.ruby-forum.com/.

I recommend learning how this works:

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation

driver.execute_script("window.open('#{link}')")

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

I generally avoid having to escape quote characters within JavaScript by
using %Q

%Q| "Some Text ' Containing " ' Quotes |

···

--
Posted via http://www.ruby-forum.com/.

@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 ?

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

Have a look at this:
http://kb.mozillazine.org/Browser.link.open_newwindow.restriction

···

--
Posted via http://www.ruby-forum.com/.

Having dug a little deeper, I found a post which stated that Selenium
Webdriver does not support opening links in a new tab:

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.

···

--
Posted via http://www.ruby-forum.com/.

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=Centralnotice"\)')

···

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1120742:

I recommend learning how this works:

Ruby Programming/Syntax/Literals - Wikibooks, open books for an open world

driver.execute_script("window.open('#{link}')")

Yes,I managed it by doing some debugging. Thanks for your help!

···

--
Posted via http://www.ruby-forum.com/\.

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. :frowning:

···

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1120756:

Have a look at this:
Browser.link.open newwindow.restriction - MozillaZine Knowledge Base

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.

···

--
Posted via http://www.ruby-forum.com/\.

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:
Firefox only supports windows not tabs · Issue #3380 · SeleniumHQ/selenium-google-code-issue-archive · GitHub

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!!

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/\.

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! :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

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 ?

···

--
Posted via http://www.ruby-forum.com/\.

Love U Ruby wrote in post #1120944:

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
url = 'https://www.google.co.in/&#39;
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.. :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.