Welcome to Selenium Webdriver Forum

As a web user you can read and search through forum content. Once you have registered and signed you can post topics, post replies, set up watch lists, modify forum settings, and use private messaging.

Author Topic: Selenium webdriver PHP trying to execute a snippet of javascript  (Read 1372 times)

Emily Isgar

  • Web User
 I am currently writing a PHP Dashboard that enables users to record a
 test via the Selenium IDE, import it into a PHP frameworked Dashboard
 and then run those tests again (at any time).
I am trying to use the execute/runScript functions to inject
 Javascript into the browser when running these tests via Selenium
 Webdriver, however I cannot seem to get it to work.
This is the function I am trying to use in PHP Webdriver:
      Inject a snippet of JavaScript into the page for execution in
 the context of the currently selected frame.
     * The executed script is assumed to be synchronous and the result
 of evaluating the script
     * is returned to the client.
     * @return Object result of evaluating the script is returned to
 the client.
    public function execute($script, $script_args) {
        $request = $this->requestURL . "/execute";
        $session = $this->curlInit($request);
        $args = array('script' => $script, 'args' => $script_args);
        $jsonData = json_encode($args);
        $this->preparePOST($session, $jsonData);
        $response = curl_exec($session);
        return $this->extractValueFromJsonResponse($response);
However I know that Selenium IDE uses the runScript command.
Whenever I try to pass any Javascript commands over to this function I
 just seem to get JAVA errors in the Selenium Log, and after spending a
 lot of time searching Google and trying different things I only seem
 to be able to find JAVA support for this functionality.
Please can anybody help?
Thank you
 Sent from my iPhone


  • Web User
Re: Selenium webdriver PHP trying to execute a snippet of javascript
« Reply #1 on: March 29, 2012, 01:55:45 PM »
Hi Emily, I am not a PHP coder, so I can't help you much here.   
 However, a couple of thoughts.
 I assume you are using WebDriver.execute($script,$script_args).
  According to the documentation, WebDriver creates an anonymous
 javascript  function, and the $script_args variable is transformed
 into a local array within that function called "arguments" and your
 javascript script can use the "arguments" variable in the code.  NOTE:
 The first argument can be accessed in javascript as "arguments[0]",
 the second argument can be accessed as "arguments[1]", etc…
 Also, according to the documentation, you can pass in as arguments any
 primitive data type (int, string, float, etc…) and , most importantly,
 you can pass in a WebElement object.  When a WebElement object is
 passed in, it is transformed into the corresponding DOM element.
 So, even though the "WebElement" object does not have a "setAttribute"
 method, once the WebElement has been converted into a DOM element  the
 javascript engine does have a "setAttribute" method.
 So, in essence, if I tell WebDriver to execute the following
 javascipt : "arguments[0].setAttribute(arguments[1],arguments[2]);"
  and I pass in as arguments a WebElement that I previously found (by
 using WebDriver.findElement) and two strings, the Javascript will
 properly set the attribute on the DOM element.
 Hope that helps.  If not, send the errors you keep getting and the
 script you are passing in.
 On Nov 13, 7:41 am, Emily Isgar <emily.is...@googlemail.com> wrote:

- Show quoted text -


Related Topics

  Subject / Started by Replies Last post
1 Replies
Last post April 01, 2012, 08:39:39 PM
by Luke Inman-Semerau
1 Replies
Last post May 09, 2012, 10:18:55 PM
by Mike Riley
4 Replies
Last post June 04, 2013, 03:57:24 PM
by Selenium Webdriver Admin
0 Replies
Last post January 10, 2013, 10:49:08 PM
by lokeshima
4 Replies
Last post January 27, 2013, 10:13:27 PM
by divya.madaan
2 Replies
Last post April 01, 2013, 09:30:55 AM
by Hector Flores
0 Replies
Last post April 16, 2013, 07:54:53 AM
by Hector Flores