Search code examples
pythonhtmlselenium-webdriverxpath

How to select only certain div elements in a list of div elements with selenium


I have a list of divs as follows

enter image description here

From the list of divs given below, I only want to extract certain data from divs between data-index=3 and data-index=10.

Currently I am able to extract all the divs using XPATH. But I don't want that. I want to only target specific divs.

How do I do that? Thank you


Solution

  • Use this XPath for inclusive 3 and 10:

    //div[div[@data-index]]/div[@data-index[. >= 3 and . <= 10]]
    

    Or this XPath for exclusive 3 and 10:

    //div[div[@data-index]]/div[@data-index[. > 3 and . < 10]]