Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Have you tried changing your CSS selector to .logMessage.error? – user246 Feb 11 '14 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. – Django_Tester Feb 12 '14 at 9:00

Why not use the selenium Alert switchto method and gettext?

Alert alert = driver.switchTo().alert(); 
String text = alert.getText(); 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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