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.
CodePudding user response:
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.