willmahon
willmahon

Reputation: 339

How to loop through elements with a specific class name in selenium javascript

There are a dynamic number of div containers on the page with the class name 'sponge', I need to loop through these and then get details from each one, but I can't figure out how to do this. This is what I've tried so far...

const containers = await driver.findElements(By.className(`sponge`))

for(let t = 0; t < containers.length; t++){
  let runner = await containers[t].findElement(By.xpath(`//div[2]/div[1]/div/p`)).getText()
  console.log(runner)
}

When I do this I just get the 'runner' in the first 'sponge' class container x amount of times, with x being the length of 'contaniers'. It doesn't actually find the 'runner' that is in the contanier I want

Upvotes: 2

Views: 1603

Answers (2)

Mahmoud Y3c
Mahmoud Y3c

Reputation: 94

  • i suggest you to use another way to scraping this website you can try modules like request, got, axios, node-ferch they will send a http request to the page and will getting back you the page html then you can use a module like jquery-jsdom to get the dom object of the response element then you can access anything you wants to by jQuery selectors.
  • scraping to extract data is better by http request

Upvotes: 0

KunduK
KunduK

Reputation: 33384

To get the immediate child node for parent node you need to use . try now.

const containers = await driver.findElements(By.className("sponge"))

for(let t = 0; t < containers.length; t++){
  let runner = await containers[t].findElement(By.xpath(".//div[2]/div[1]/div/p")).getText()
  console.log(runner)
}

Upvotes: 2

Related Questions