Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element by cssSelector in Selenium (Java)

Tags:

java

selenium

<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.

like image 558
Sugan Avatar asked Dec 02 '14 06:12

Sugan


People also ask

How can I locate an element using Cssselector?

We can locate an element by using the starting text of the element. It is quite useful if you know the starting text of the element attribute. We can use the starting character sequence of the attribute value to locate the element using CSS Selectors. Here, we have used the id attribute.

How do you write Cssselector in Selenium?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.

What is the difference between XPath and Cssselector?

We can traverse both forward and backward in DOM, i.e we can move from parent to child element and also from child to the parent element with xpath. However for css, we can only traverse from parent to child and not vice-versa. In terms of performance, css is better and faster, while xpath is on a slower side.

What is Cssselector?

A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.


1 Answers

You can use it by element selector

By.cssSelector("input[name=email]")
like image 183
Murtaza Khursheed Hussain Avatar answered Sep 26 '22 15:09

Murtaza Khursheed Hussain