Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse returned element with nodeJS and Selenium

For the first time I’m trying Selenium and nodeJS. I need to parse HTML page and I’m having problem to parse returned element with XPATH again, to perform deeper search. Here is my code:

async function parse () {
    let driver = await new Builder().forBrowser("firefox").build();

    await driver.get("https://web-url.com");
    await sleep(2000);  //custom function

    let elements = await driver.findElements(By.xpath("//ul[contains(@class, 'some-class')]//li"));

    for (let i = 0; i < elements.length; i++) {
        let timeContainer = await elements[i].findElement(By.xpath("//time[contains(@class, 'time-class')]")).getAttribute("datetime");
        let innerHTML = await elements[i].getAttribute("innerHTML");

        console.log(timeContainer);
    }

    await sleep(2000); //custom function
    driver.close();
}

innerHTML variable returns correct HTML string for each element, but if I try to print timeContainer var, it prints same value for all elements. How to perform additional XPATH search for each element?

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to make the XPath in this line elements[i].findElement(By.xpath("//time[contains(@class, 'time-class')]")) relative.
This is done by putting a dot . before the rest of XPath expression, as following:

elements[i].findElement(By.xpath(".//time[contains(@class, 'time-class')]"))

By default, when the page is scanned to find the match to the passed XPath expression it starts from the top of the page. This is why this line returns you the first (the same) match each time.
But when we put a dot . there, it starts searching inside the current node i.e. inside the elements[i] element.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading