1

I have below HTML code in my project.

<button id="tab-oneWay" class="" data-initial-tab="true" type="button" data-section-id="#section-oneWay">
  <span class="tab-label">
    One Way
      <span class="visuallyhidden"> Tab 1 of 2</span>
  </span>
</button>

I have to find only the text 'One Way'. I have tried below codes but it is returning 'One Way Tab 1 of 2'.

findElement(By.id("tab-oneWay")).getText()
findElement(By.cssSelector(".tab-label")).getText()

I am new in selenium so not sure how to find this. Any help on this would be great ! Thanks in advance

ankit
  • 23
  • 1
  • 5
  • 1
    There are no so simple solution. Here is similiar thread: http://stackoverflow.com/questions/28945692/how-to-get-text-from-parent-element-and-exclude-text-from-children-c-selenium You can for example find children's text and remeve it from parent's text – kotoj Jun 20 '16 at 16:16
  • Thanks for the thread. I was trying to avoid string calculation but looks like i have to do. Anyway thanks for the answer. – ankit Jun 21 '16 at 04:14

2 Answers2

0

findElement(By.xPath("/button/span[contains(., 'One Way')]").getText()

Could you try finding the element using XPath, as above, to see if that returns correctly?

stuartmccoll
  • 188
  • 1
  • 11
  • This is also giving the whole text (One Way Tab 1 of 2) instead of what i am looking. Thanks for the help. – ankit Jun 21 '16 at 04:15
0

Try this:

spans = find_elements_by_tag_name("span")
span1_text = spans[0].get_text()
Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
Di_Moore
  • 11
  • 4