Question

I have the following HTML code:

div id="flashMessage" class="error"> 
<span>Saved Section.</span>
<div id="errors" class="clearfix">
</div>

I would like to get the text which is contained in span, I tried all the locators.

Could someone please help me with the respective command and css or xpath locator in selenium, for the above query?

Was it helpful?

Solution

This should work for you:

string spanText = driver.findElement(By.cssSelector("div#flashMessage>span")).getText();

I'm not sure why the same locator wouldn't work for success messages, but you can try this:

string spanText = driver.findElement(By.cssSelector("div#flashMessage.msg>span")).getText();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top