I have the following DOM structure / HTML:
<div class = "something" >
<div>
<span>Some text</span>
</div>
"Automated Script"
</div>
I am testing using Selenium Webdriver and have to fetch the string Automated Script to do an Assert.
Assert.assertEquals("Automated Script",webDriver.findElement(By.xpath("/div")).getText());
But the above xpath fetches both Some Text and Automated Script. Is there a way to only get the string Automated Script?
With assertEquals
:
String divText = webDriver.findElement(By.cssSelector("div.something")).getText();
String spanText = webDriver.findElement(By.cssSelector("div.something span")).getText();
String expectedText = divText.replace(spanText, "").strip();
Assert.assertEquals("Automated Script", expectedText);
With contains and asserTrue
:
Assert.assertTrue(webDriver.findElement(By.cssSelector("div.something")).getText().contains("Automated Script"));
Using WebDriverWait
, that will wait for text and through an error:
new WebDriverWait(driver, 5)
.until(ExpectedConditions
.textToBePresentInElementLocated(By.cssSelector("div.something"), "Automated Script"));