Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Selenium: Trying to resize window from Perl: getting a mysterious JavaScript error

by HelenCr (Monk)
on Jul 05, 2014 at 10:17 UTC ( #1092355=perlquestion: print w/replies, xml ) Need Help??
HelenCr has asked for the wisdom of the Perl Monks concerning the following question:

Dear esteemed PerlMonks

I am using the Perl Selenium package, WWW::Selenium.

Trying to resize the browser window, I am getting a mysterious JavaScript error: "Threw an exception: missing ; before statement".

Here is the code:

use strict; use warnings; use 5.014; use autodie; use warnings qw< FATAL utf8 >; use Carp; use Carp::Always; use WWW::Selenium; my $url = q{}; #for example my $sel = WWW::Selenium->new( host => 'localhost', port => 4444, browser => '*firefox F:\WIN 7 programs\Web & Internet\Firefox 8 bit +\firefox.exe', browser_url => $url, ); $sel->open( $url ); $sel->wait_for_page_to_load(10000); my $res = $sel->window_maximize(); # So far, this works fine $res = $sel->get_eval( q{ WebDriver driver = ((WebDriverBackedSelenium +) selenium).getWrappedDriver(); driver.manage().window().setSize(1040,768);} ); # (Following this:, Eli + Colner's post)

The program then crashes here with: "Threw an exception: missing ; before statement"

If I drop the first JavaScript line and just leave in the 2nd line, namely:

$res = $sel->get_eval( q{driver.manage().window().setSize(1040,720);} );

It bumps with: "driver not defined".

Help will be appreciated - Thanks in advance


Note: cross posted here:

Replies are listed 'Best First'.
Re: Selenium: Trying to resize window from Perl: getting a mysterious JavaScript error
by Corion (Pope) on Jul 05, 2014 at 12:58 UTC

    The code you're passing to ->get_eval looks like Java code. Are you certain that driver is a variable defined by the Selenium Javascript driver?

      Thank you, Corion, for your trustworthy help, as always.
      This snippet in the OP:

      $res = $sel->get_eval( q{ WebDriver driver = ((WebDriverBackedSelenium +) selenium).getWrappedDriver(); driver.manage().window().setSize(1040,768);} );

      does define "driver" ok.

      As you can see, when replacing it with:

      my $js = q{((WebDriverBackedSelenium) selenium).getWrappedDriver().man +age().window().setSize(1040,720);} ; $sel->get_eval($js):

      (as suggested in Matthias' comment here:,
      it produces the error:

       missing ) in parenthetical

      This is typically a javascript error not pointing to a missing parentheses, but instead to invalid javascript, for some reason (sometimes: bad string representation)
      (see Slaks' comment here:

      This leads me to think that somehow, the Perl WWW::Selenium module is not passing on right the javascript string to the Selenium server.

      See here, too:

        I see invalid javascript in your code, you made a mistaken assumption. Regarding the referenced SO thread that you base your code on:

        what makes you think Eli Corner's answer/solution is "javascript"? That is Java, or C# otherwise, because only those language bindings for WebDriver (or Selenium 2) expose a WebDriverBackedSelenium feature. All other language bindings, including Perl have no such option. So even if the code syntax is correct, on execution it will fail because that's not javascript (or shall I say the referenced classes/objects are not javascript).

        Your options for a solution the way I see it are:

        1. use real javascript code and Dave Hunt's solution (in that same SO thread) ideally should work, adapted for Perl:

        $sel->get_eval("window.resizeTo(1024, 768); window.moveTo(0,0);");

        2. use Perl WebDriver binding to correctly use Eli Corner's solution (adapted for Perl), not Selenium (RC) binding that you are currently using. Perl WebDriver binding is Selenium::Remote::Driver, not WWW:Selenium. You should then be able to do something like this (there is no need for the WebDriverBackedSelenium part in Perl, but it does mean you have to switch off using Selenium RC moving to WebDriver, there's no backward compatibility support, you need Java or C# for that):

        $driver->set_window_position(0, 0);

        $driver->set_window_size(640, 480);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1092355]
Front-paged by tye
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2018-04-19 20:16 GMT
Find Nodes?
    Voting Booth?