Vitaliy
Vitaliy

Reputation: 359

How to find all links on a page in node.js?

I'm studing with node.js (webdriver) right now, and can't understand some issues. I have tried to search in internet and didn't find any examples of task I needed. For example, how I can log to console all links from the page (bing.com for example)? I don't understand why simple javascript constructions like this

document.getElementsByTagName('a');

doesn't work in node.js.

If I use

findElement(webdriver.By.tagName('a'))

I got only first link on the page, why? Arrays not available in nodejs?

If it's not possible, which module should be used (example). For current moment, I have the impression that for even a simple functionality an additional modules required, therefore all my first steps failed, because I'm trying do anything like in javascript.

Thanks

Upvotes: 2

Views: 2286

Answers (2)

Vitaliy
Vitaliy

Reputation: 359

I have found answer on my question and answered in this topic Click on random link via node.js and webdriver

Example of code (thanks to Nguyen Vu Hoang)

driver.findElements(webdriver.By.tagName("a")).then(function(elems){
    elems.forEach(function (elem) {
        elem.getText().then(function(textValue){
            console.log(textValue);
        });
    });
});

Upvotes: 2

Haojie
Haojie

Reputation: 5713

Please documentation about findElements

findElements ( locator ) ⇒ !webdriver.promise.Promise

Schedules a command to find all of the descendants of this element that match the given search criteria. Parameters

locator: !(webdriver.Locator|webdriver.By.Hash|Function)

The locator strategy to use when searching for the elements.

Returns

A promise that will resolve to an array of WebElements.

Upvotes: 0

Related Questions