JohnDotOwl
JohnDotOwl

Reputation: 3755

WebDriverJs' getPageSource gives me an object rather than the page's source

res.json is returning blank when I try display the source of the page. The Selenium log shows that the source code was retrieved. Any idea how I can receive the updated page source properly?

It returns the same thing if I use other functions like get current url.

Code:

var driver = new webdriver.Builder().usingServer('http://localhost:4444/wd/hub').withCapabilities(webdriver.Capabilities.firefox()).build();
     driver.get('http://www.google.com');
     var source = driver.getPageSource();
        console.log(source);
        res.json({ message: source });

console.log outputs:

 [{ then: [Function: then],                                                                                                                                                         
      cancel: [Function: cancel],                                                                                                                                                       
      isPending: [Function: isPending] }

Selenium log:

16:17:30.273 INFO - Executing: [new session: Capabilities [{browserName=firefox}]])                                                                                                 
16:17:30.286 INFO - Creating a new session for Capabilities [{browserName=firefox}]                                                                                                 
16:17:39.751 INFO - Done: [new session: Capabilities [{browserName=firefox}]]                                                                                                       
16:17:39.862 INFO - Executing: [get: http://www.google.com])                                                                                                                        
16:17:43.828 INFO - Done: [get: http://www.google.com]                                                                                                                              
16:17:43.863 INFO - Executing: [get page source])                                                                                                                                   
16:17:44.036 INFO - Done: [get page source]                                                                                                                                         
16:17:44.081 INFO - Executing: [delete session: d816aa4f-f5ad-4a59-aec0-4475cab4dff1])                                                                                              
16:17:44.206 INFO - Done: [delete session: d816aa4f-f5ad-4a59-aec0-4475cab4dff1]  

Upvotes: 1

Views: 3128

Answers (1)

Louis
Louis

Reputation: 151401

source is a promise to get the source, not the source itself. I'd expect you have to do something like:

source.then(function (src) {
    res.json({ message: src });
});

The clue there is that when you output source to the console, you get an object that has then, cancel, and isPending methods. The then method is very often used by promise frameworks to pass the callback that will be called when the promise is resolved.

Upvotes: 2

Related Questions