6
<html>
    <body>    
        <div id="login-box" class="form-box">    
            <form id="frmlogin" class="form" name="frmlogin" method="post">
                <div class="body">
                    <div class="form-group">
                        <input id="email" class="form-control" type="text" maxlength="50"     value="[email protected]" placeholder="Email" name="email">
                        <span class="red">Please provide a valid email address</span>
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="password" maxlength="15" placeholder="Password" name="password">
                        <span class="red">Password must not be empty</span>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

I need to get "Please provide a valid email address" and "Password must not be empty" using nth-child in cssSelector.

I tried the below snippet:

//Case 2

    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)>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");
    }

It results in NoSuchElementFound.

2
  • The span elements are the 2nd child of the form-group element. Try using .form-group>span:nth-child(2) as your selector. Commented Dec 2, 2014 at 9:23
  • Thank You Mark Rowlands. I tried out with this already but I din't get the output Commented Dec 2, 2014 at 10:30

5 Answers 5

3

You can use it by element selector

By.cssSelector("input[name=email]")
Sign up to request clarification or add additional context in comments.

2 Comments

Specifying more details such as where to change the code and how the changed code helps solve the OP's problem will make the answer more acceptable.
If you have read question clearly, Sugan has said that "String a=driver.findElement(By.cssSelector(".form-group>span:nth-child(1)")).getText(); It results in NoSuchElementFound.". I exactly answer that.
1

Try these codes below:

1- For returning 'Please provide a valid email address':

String a = driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(1)>span")).getText();
System.out.println(a);

2- For returning 'Password must not be empty':

String a = driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(2)>span")).getText();
System.out.println(a);

13 Comments

Thank you Subh. Even if I am using the above code Is results in same error
I have modified the css selector. Please check now @Sugan
It throws me invalid Selector org.openqa.selenium.InvalidSelectorException
I think there is a problem with how you are implementing it. Can you please add the relevant code in your question ? Also, more of the html snippet will be helpful too. Because when I run with the existing snippet and my code, it is working fine and returning the expected outputs. @Sugan
I have given the code and Snippet. I don't know whether I am missing out anything could me help me out
|
1

If you don't have to you could also use XPath selectors which are in my opinion easier to use when you have to select the nth element of something. Try this one:

For E-Mail:

By.xpath("//div[@class='form-group'][1]/span")

For the password:

By.xpath("//div[@class='form-group'][2]/span")

But could you also provide the full HTML page you are working with? Maybe the form is inside an iframe and therefore Selenium has problems locating the element

2 Comments

Thank you Stefan. I tried with this also it shows me the same error. No frames have been used in the HTML page.
BTW xpath are slower and much slower in IE
0

You need to add .getText() method in order to get the text inside the tag.

Comments

-1

Have you tried something like object.getAttribute("innerHTML"), literally with "innerHTML", like this ?

I had a similar problem getting a text from a span tag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.