Click on first element contained in a div

267 Views Asked by At

I have a div that contains a set of dynamic elements. I want to click on the first search result.

Here is what the HTML looks like.

I want to click on the first element contains in

I tried using creating a custom xPath like so but it didn't work. Any ideas here?

//div[1][contains(text(), 'listing')]
2

There are 2 best solutions below

0
Tohirul Islam On BEST ANSWER

First of all It would've helped if you had provided more information.

best will be using pseudo-child like div.firstChild or if the elements are generated dynamically you can add a class and use document.querySelectorAll(".class") which will give you an array of elements that had the class.

You can use array[0] to use click on the first element.

0
theodoros On

For anyone coming across this thread here is the solution

const listings = await page.$x('//*[contains(@id,"listing_")]')