I am writing a script in node.js for selenium that will go to a page and grab the innerhtml of a certain css class and store them in an array.
var element =
To retrieve the html of multiple elements, you can use driver.findElements() to find all matches elements. This will provider a Promise
that resolves with the elements in an Array
.
var pendingElements = driver.findElements(By.className('h11'))
pendingElements.then(function (elements) {
// ...
});
You'll need to iterate over the collection and request each element's HTML. You can use the Array
's .map() to create a collection of promises from getInnerHtml()
:
var pendingHtml = elements.map(function (elem) {
return elem.getInnerHtml();
});
To wait for them to be resolved, you can pass the collection to promise.all().
promise.all(pendingHtml).then(function (allHtml) {
// ...
});
Note, you'll need a reference to Selenium's promise
for that.
var promise = require('selenium-webdriver').promise;
Combined:
// ...
var promise = require('selenium-webdriver').promise;
var pendingElements = driver.findElements(By.className('h11'))
pendingElements.then(function (elements) {
var pendingHtml = elements.map(function (elem) {
return elem.getInnerHtml();
});
promise.all(pendingHtml).then(function (allHtml) {
// `allHtml` will be an `Array` of strings
});
});