11

I'm interested in executing a number of the advanced "Commands" via the javascript API https://code.google.com/p/selenium/source/browse/javascript/webdriver/command.js

If I start with the base code:

var browser = new webdriver
        .Builder()
        .usingServer(server.address())
        .withCapabilities(webdriver.Capabilities.phantomjs())
        .build();

every form of "likely" syntax I've tried to execute has failed. for example:

// does not work
console.log(webdriver.Command('getWindowSize'))
// does not work
console.log(browser.Command('getWindowSize'))

Does anyone know how to execute "get window size", or "set window size" in selenium javascript webdriver?

3
  • I haven't used the Command stuff, but we have used the ExecuteScript function to execute JavaScript. If I recall, it returns whatever the javascript would normally return, so just excecute a javascript function that returns the window size (I think you might have to do width an height separately or get them separately and return them as an object with 2 values or something.)
    – Pete
    Commented Sep 30, 2013 at 21:23
  • 1
    Thanks Pete. console.log(browser.ExecuteScript('window.height')); in that context doesn't seem to work. Nor does console.log(browser.executor_.execute('window.height')); you have any samples ?
    – Alex C
    Commented Sep 30, 2013 at 21:30
  • 2
    Alex, I've had so many issues, similar to that which you're encountering. Selenium-webdriver in Node.js is a mess. The whole promises system seems to sort of make sense, but I've yet to find solid real-life examples (beyond simple "hello world" stuff) of how to use it. I've been trying for the last day to simply get the visible text of an element... (sigh) I think I'm going to bail on selenium-webdriver, and use webdriver.io instead. Looks promising, and appears to use the already-familiar chaining syntax to approximate synchrony.
    – rinogo
    Commented Jan 24, 2014 at 17:20

3 Answers 3

28

You are probably looking for driver.executeScript.

Example:

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.executeScript('return 2').then(function(return_value) {
    console.log('returned ', return_value)
});

This will log 2 to the console.

I also tested this with:

driver.get('http://underscorejs.org/');

driver.executeScript('return _').then(function(return_value) {
    console.log('returned ', return_value)
});

... which correctly lists all the methods defined on _, so it appears to work.

1
0

In my npm package there's a file node_modules/selenium-webdriver/test/execute_script_test.js with a great many examples using driver.executeScript (at the very end of the file they define execute as an alias for executeScript).

4
  • While linking to external resources is fine, an answer should be self-contained, and the external resource shouldn't be strictly necessary, but rather additional content (actually, this answer doesn't even contain said link, but merely mentions "my npm package"). Commented Aug 30, 2016 at 0:15
  • It was not my intention to answer the question. I felt this had already been done. I intended to point out a useful, supplementary resource which users of selenium-webdriver would already have in the npm module they installed.
    – AdamW
    Commented Aug 30, 2016 at 15:29
  • 1
    I see now that I could have, probably should have, added this info as a comment rather than an answer
    – AdamW
    Commented Aug 30, 2016 at 17:32
0

You need driver.executeScript(). You can either pass a function or a string.

const _scraper = () => {
  return document.title;
}

const getPageTitle = async (url) => {
  await driver.get(url);
  const pageTitle = await driver.executeScript(_scraper);
  console.log(pageTitle);
}

You can also pass argument to the function.

const _scraper1 = (arg) => {
  return document.getElementsByTagName(arg);
}

const getTagData = async (url) => {
  await driver.get(url);
  const metas = await driver.executeScript(_scraper, 'meta');
  console.log(metas);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.