I am using WebDriver and I need to use XPath to get the text that appears after the <br></br> which is "59Min" from the below HTML.


2Hrs, 56Min



<input value="3hrs"></input>
<input value="4hrs"></input>

The following code doesn't do it:

var dfs = driver.FindElement(By.XPath("//table[@id='PowerReserveTables2_0']/tbody/tr/td[6]/div/text()[2]")).Text;

Neither if I use following-sibling like this:

  • /div/input/preceding-sibling::[2] Your question is vague but this might lead you in the right direction – Paul Muir Jun 24 '14 at 23:53
  • Thanks, but that didn't work either. I notice part of my question was chopped off. I am trying to locate the text that appears after the br tags. WebDriver, by nature, always locates elements, but techniques such as "following-sibling::text()" or "div/text()[2]" locate text. They are valid, but don't work in WebDriver. – MahlerAdmirer Jun 25 '14 at 13:23
  • 1
    In the above code, the "2Hrs, 56Min" and "59Min" are both text within the div tag. In order to be a sibling, the div tag would have to be closed and the second string "59Min" would need to be within another tag. To get the "59Min" simply get the text of the div, and then parse the text to find the characters after the last white space. – Sam Woods Jun 25 '14 at 15:30

Try this:

var dfs = driver.FindElement(By.XPath("//table[@id='PowerReserveTables2_0']/tbody/tr/td[6]/div")).Text.split("<br></br>")[1].trim();
  • +1 because I think this is the correct answer. Getting the text of the full <div> and then splitting the text in <br><br> will get two items where the second is the text the OP wants. @Dmitry although the answer is good, in the future maybe a little bit more of context why your solution will work. I think most readers here are not developers :) – Niels van Reijmersdal May 11 '15 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.