Using WebDriver and JUnit 4.11. I'm attempting to find an alert and assert the body of the alert paragraph.

My code is:

public void dNameField(){

    WebElement nameField = chrome.findElementByCssSelector("input.t-textInput-A[name='name']");

    List<WebElement> elements;
    elements = chrome.findElements(By.className("logMessage.error"));

    Set<String> foundAlerts = new HashSet<String>();

    for(WebElement e : elements){

    Assert.assertTrue("This should be an error message", foundAlerts.contains("An error has occurred : Value must be a Number."));

I receive a java.lang.AssertionError: in the stacktrace.

In the DOM before an alert is thrown this is this class which holds all the error logs that are produced in the session. <ul class="logContainer t-logs-A">

Once I have managed to simulate an alert by clearing all data from a particular field, the alert is thrown in this class

<ul class="logContainer t-logs-A">
<li class="logMessage error" id="l1392048208775" style="display: none;">
<p class="closeBtn"></p>
<p>An error has occurred : Value must be a Number.</p>

The id is dynamically created so when I attempt to 'copy xpath' I'm supplied with //*[@id="l1392048208775"]/p which is no good.

The alerts that are thrown all remain in the DOM as I can visibly see them, although they are hidden as they fade out from the UI after a couple of seconds.

Any suggestions on how to go about this, please?

  • 1
    Have you tried changing your CSS selector to .logMessage.error?
    – user246
    Feb 11, 2014 at 0:44
  • Hi @user246 I tried placing a dot before the CSS selector and that did nothing, as I suspected it would. But thanks anyway. Feb 12, 2014 at 9:00

1 Answer 1


Why not use the selenium Alert switchto method and gettext?

Alert alert = driver.switchTo().alert(); 
String text = alert.getText(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.