The text you want is present in the text node and cannot be obtained directly using Selenium, since it only supports node elements.
You can remove the beginning:
String buttonText = driver.findElement(By.cssSelector("div.success > button")).getText();
String fullText = driver.findElement(By.cssSelector("div.success")).getText();
String text = fullText.substring(buttonText.length());
You can also extract the desired content from innerHTMLusing a regular expression:
String innerText = driver.findElement(By.cssSelector("div.success")).getAttribute("innerHTML");
String text = innerText.replaceFirst(".+?</button>([^>]+).*", "$1").trim();
Or with a piece of JavaScript:
String text = (String)((JavascriptExecutor)driver).executeScript(
"return document.querySelector('div.success > button').nextSibling.textContent;");
source
share