seleniumxpathnextsibling

Selenium XPATH selecting next sibling


<div class="block wbc">
   <span></span>
   <span> text_value </span>
</div>

for getting text in second span where does below code go wrong?

driver.find_element(X_PATH,"*//div[@class='block']/span[1]")

For trying by yourself, maybe I write sth wrong here is link

https://soundcloud.com/daydoseofhouse/snt-whats-wrong/s-jmbaiBDyQ0d?si=233b2f843a2c4a7c8afd6b9161369717&utm_source=clipboard&utm_medium=text&utm_campaign=social_sharing

And my code; still giving an error

playbackTimeline__duration =driver.find_element(By.XPATH,"*//div[@class='playbackTimeline__duration']/span[2]")

For finding web element clearly:

//*[@id="app"]/div[4]/section/div/div[3]/div[3]/div/div[3]/span[2]

But I will not use this way, I need declare with class method or CSS Selector at least


Solution

  • If you are sure that you always need the second span use this XPath:

    *//div[@class='playbackTimeline__duration']/span[2]
    

    If you need the first span that has actual text use this:

    *//div[@class='playbackTimeline__duration']/span[normalize-space()][1]
    

    If the @class has more than only playbackTimeline__duration in it you can use:

    *//div[contains(@class,'playbackTimeline__duration')]/span[2]
    

    If there are more div's like that use:

    *//div[contains(@class,'playbackTimeline__duration')][1]/span[2]