WebDriverJs的getPageSource给了我一个对象,而不是页面的源代码

当我尝试显示页面的源时, res.json返回空白。 Selenium日志显示源代码已被检索。 任何想法如何才能正确接收更新的页面源代码?

它返回相同的东西,如果我使用其他function,如获取当前url。

码:

 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输出:

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

selenium日志:

 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] 

source承诺获得来源,而不是来源本身。 我希望你必须做一些事情:

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

这里的线索是,当你输出source到控制台时,你会得到一个对象, then cancelisPending方法。 promise框架经常使用then方法来传递在promise被parsing时调用的callback函数。