1

My HTML looks like :

<div id="cc" class="col-md-6" _ngcontent-c12="">
  <h5 _ngcontent-c12="">Blah Blah</h5>
  My Answer
</div>

When I use :

driver.findElement(By.id("cc")).getText()

It returns me "Blah Blah My Answer"

I need only "My Answer" to be returned. Is there a way to do that through Selenium WebDriver?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Melvin Richard
  • 403
  • 1
  • 7
  • 15
  • 2
    Possible duplicate of [How to use selenium get Text from an element not including it's sub-element](https://stackoverflow.com/questions/39741076/how-to-use-selenium-get-text-from-an-element-not-including-its-sub-element) – achAmháin Apr 26 '18 at 06:45

1 Answers1

0

As per the HTML you have provided to retrieve the text My Answer you can use the following code block :

WebElement elem = driver.findElement(By.id("cc"));
String mytext = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[2].textContent;", elem);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352