1

I'm trying to grab HTML from a web page using JavaScript and Selenium behind Nodejs. The Webdriver is using Promises and no matter what I try, all I seem to be able to access is the promise. I started down this path thinking that

driver.page_source

would work, but that was a non-starter both before the promise and the in the callback.

Here's my code:

var webdriver = require("selenium-webdriver");

var urlRoot = "http://www.google.com/finance";

var createWebDriver = function() {
    return new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
};

exports.financials = function(symbol, callback) {
    var driver = createWebDriver();
    driver.get(urlRoot + "?q=" + symbol);

    driver.findElement(webdriver.By.linkText("Financials")).click();

    console.log("content? = " + driver.findElement(webdriver.By.tagName("body")).getAttribute("innerHTML"));
    console.log("table? = " + driver.findElement(webdriver.By.id("fs-table")).getAttribute("outerHTML"));
    var contentPromise = driver.getPageSource();

    contentPromise.then(function() {
        console.log("contentPromise = " + contentPromise);
        console.log("content? = " + driver.findElement(webdriver.By.id("fs-table")).getAttribute("outerHTML"));
        driver.quit();
        return callback(null, contentPromise);
    });
};

The output is:

  fetchFinancials
content? = Promise::90 {[[PromiseStatus]]: "pending"}
table? = Promise::119 {[[PromiseStatus]]: "pending"}
contentPromise = Promise::130 {[[PromiseStatus]]: "fulfilled"}
content? = Promise::360 {[[PromiseStatus]]: "pending"}

Does anyone know how to extract the HTML from the promise and/or driver?

Thanks in advance.

Vic F
  • 1,143
  • 1
  • 11
  • 26
  • ex: `contentPromise.then(console.log);` – dandavis Apr 12 '15 at 05:11
  • @dandavis, not sure what you're getting at there. – Vic F Apr 13 '15 at 13:21
  • the snip i showed didn't work? – dandavis Apr 14 '15 at 01:05
  • @dandavis, I realized that my knowledge and skills with promises were lacking, so I redirected my attention to understanding promises; I think that's where breakdown is for me. I have posted another promises-related question [here](http://stackoverflow.com/questions/28978485/mongoose-promise-and-q-promise). – Vic F Apr 16 '15 at 13:33

0 Answers0