1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! It did a job. I was looking for problems in JS and Selenium and totally ignored XPATH syntax part...
I'm happy I could help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.