Search code examples
selenium-webdriverxpathaxes

need xpath where the next descendant that has text is returned


I am trying to write a locator where the next text descendant is returned. I wont know the text. The following xpath works:

//*[@id='myChart']//label[contains(text(),"Show:")]/following::div[4]

but I dont like the div[4] as this could easily change. The element is the first div type descendant under show that contains text. Any suggestions?

A


Solution

  • Considering the following clauses:

    • the next text descendant
    • I wont know the text
    • div[4] as this could easily change
    • element is the first div type descendant

    To locate the element a couple of effective approaches are as follows:

    • Using xpath:

      //*[@id='myChart']//label[contains(., "Show")]//div[text()]
      
    • Using xpath with descendant:

      //*[@id='myChart']//label[contains(., "Show")]//descendant::div[text()]
      
    • Using xpath with following:

      //*[@id='myChart']//label[contains(., "Show")]//following::div[text()]