Home > Net >  need xpath where the next descendant that has text is returned
need xpath where the next descendant that has text is returned

Time:08-31

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

CodePudding user response:

I think this will work for you:

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

To give more confident answer we need to see the actual page / XML.
In case the desired div is a direct child of the label containing the "Show:" the above expression can be presided to

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

CodePudding user response:

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()]
    
  • Related