Webdriver findElements By xpath

后端 未结 3 703
悲&欢浪女
悲&欢浪女 2020-12-31 22:57

1)I am doing a tutorial to show how findElements By xpath works. I would like to know why it returns all the texts that following the

element with a
3条回答
  •  甜味超标
    2020-12-31 23:34

    The XPath turns into this:

    Get me all of the div elements that have an id equal to container.

    As for getting the first etc, you have two options.

    Turn it into a .findElement() - this will just return the first one for you anyway.

    or

    To explicitly do this in XPath, you'd be looking at:

    (//div[@id='container'])[1]
    

    for the first one, for the second etc:

    (//div[@id='container'])[2]
    

    Then XPath has a special indexer, called last, which would (you guessed it) get you the last element found:

    (//div[@id='container'])[last()]
    

    Worth mentioning that XPath indexers will start from 1 not 0 like they do in most programming languages.

    As for getting the parent 'node', well, you can use parent:

    //div[@id='container']/parent::*
    

    That would get the div's direct parent.

    You could then go further and say I want the first *div* with an id of container, and I want his parent:

    (//div[@id='container'])[1]/parent::*
    

    Hope that helps!

提交回复
热议问题