来自Javascript API的Selenium WebDriverJS

时间:2014-09-19 22:39:34

标签: javascript node.js selenium selenium-webdriver webdriver

苦苦挣扎,同时理解WebDriverJS和承诺......大部分示例代码都是针对Python / Java而不是Javascript。我试图做的就是获取页面的完整html。因此,如果您查看WebDriverJS的相同代码:

var webdriver = require('selenium-webdriver');
...
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
 return driver.getTitle().then(function(title) {
   return title === 'webdriver - Google Search';
 });
}, 1000);

我试图简单地返回整个html文档而不仅仅是标题。在Python中,它是driver.page_source。我从例子中学到了很多 ,所以我在这里有点尴尬。

1 个答案:

答案 0 :(得分:3)

如果文档不清楚,我通常更喜欢浏览源代码。

此处包含相关webdriver.js source的主getPageSource() function

/**
 * Schedules a command to retrieve the current page's source. The page source
 * returned is a representation of the underlying DOM: do not expect it to be
 * formatted or escaped in the same way as the response sent from the web
 * server.
 * @return {!webdriver.promise.Promise.<string>} A promise that will be
 *     resolved with the current page source.
 */
webdriver.WebDriver.prototype.getPageSource = function() {
  return this.schedule(
      new webdriver.Command(webdriver.CommandName.GET_PAGE_SOURCE),
      'WebDriver.getAllWindowHandles()');
};