简体   繁体   中英

Selenium WebDriverJS from Javascript API

Struggling a bit to simultaneously understand WebDriverJS and promises... and most of the sample code out there is for Python/Java, not Javascript. All I'm trying to do is to get the full html for a page. So if you look at the same code for 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);

I'm trying to simply return the entire html document instead of just the title. In Python that'd be driver.page_source. I learn much better from examples so I'm a bit flummoxed here.

I usually prefer to explore the source code in case the documentation is not clear.

Here's the the main webdriver.js source that contains relevant 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()');
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM