title: Get element by cssSelector in Selenium (Java)
body: <pre><code>&lt;html&gt;
      &lt;body&gt;    
          &lt;div id="login-box" class="form-box"&gt;    
              &lt;form id="frmlogin" class="form" name="frmlogin" method="post"&gt;
                  &lt;div class="body"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;input id="email" class="form-control" type="text" maxlength="50"     value="dfsf@gmail.com" placeholder="Email" name="email"&gt;
                          &lt;span class="red"&gt;Please provide a valid email address&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          &lt;input class="form-control" type="password" maxlength="15" placeholder="Password" name="password"&gt;
                          &lt;span class="red"&gt;Password must not be empty&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I need to get "Please provide a valid email address" and "Password must not be empty" using <code>nth-child</code> in <code>cssSelector</code>.</p>
  
  <p>I tried the below snippet:</p>
  
  <p>//Case 2</p>
  
  <pre><code>    driver.findElement(By.name("email")).clear();
      driver.findElement(By.name("email")).sendKeys("");
      String a=driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(1)&gt;span")).getText();
      System.out.println(a);
      if(a.contains("valid email address"))
      {
          System.out.println("Login test case2 Passed");
      }
      else
      {
          System.out.println("Login test case2 Failed");
      }
  </code></pre>
  
  <p>It results in <code>NoSuchElementFound</code>.</p>