Son Of Earth
Son Of Earth

Reputation: 35

Selenium Webdriver: Cant find element by classname

I am currently struggling with Selenium to select an element by its class name. The Website I'm using is:

http://demo.guru99.com/test/login.html
<div class="col-xs-12 col-sm-6">
            <form action="success.html" method="post" id="login_form" class="box">
                <h3 class="page-subheading">Already registered?</h3>
                <div class="form_content clearfix">
                    <div class="form-group">
                        <label for="email">Email address</label>
                        <input class="is_required validate account_input form-control" data-validate="isEmail" type="text" id="email" name="email" value="">
                    </div>
                    <div class="form-group">
                        <label for="passwd">Password</label>
                        <span><input class="is_required validate account_input form-control" type="password" data-validate="isPasswd" id="passwd" name="passwd" value=""></span>
                    </div>
                    <p class="lost_password form-group"><a href="#" title="Recover your forgotten password" rel="nofollow">Forgot your password?</a></p>
                    <p class="submit">
                        <input type="hidden" class="hidden" name="back" value="my-account">                     <button type="submit" id="SubmitLogin" name="SubmitLogin" class="button btn btn-default button-medium">
                            <span>
                                <i class="icon-lock left"></i>
                                Sign in
                            </span>
                        </button>
                    </p>
                </div>
            </form>
        </div>

I figured out how to select the element by id but whenever I try to select it by classname I get an element not found exception. I don't really understand how this works as I tried multiple websites without success. In this example the element has multiple classes but on some websites this method even fails when there is only one class. On this website selecting it by only one class works but I have read that this isn't a good method as you could be selecting multiple elements by mistake.

IWebElement search = driver.FindElement(By.ClassName("account_input"));

Here is my code:

driver = new ChromeDriver();          
driver.Url = "http://demo.guru99.com/test/login.html";
driver.Navigate();
IWebElement search = driver.FindElement(By.ClassName("is_required.validate.account_input.form-control"));
search.SendKeys("test");

In this example selecting element by Id using the By.Id("first_name") method doesn't work either:

<span>
      <label class="first_name_label form_label_text"></label>
                <div class="clear"></div>
                <div class="form-item icon">
                  <input value="" data-error="An error has occured." id="first_name" type="text" name="first_name" class="form_item  is_required" placeholder="First Name">
                  <i style="color:rgba(70,75,106,0.5)" class="vs icon-user"></i>                </div>
                                <div class="form-item icon">
                  <input value="" data-error="An error has occured." id="last_name" type="text" name="last_name" class="form_item  is_required" placeholder="Last Name">
                  <i style="color:rgba(70,75,106,0.5)" class="vs icon-user"></i>                </div>
<div class="clear"></div>
</span>

What am I doing wrong? Help would be greatly appreciated

Upvotes: 0

Views: 19840

Answers (3)

Apps Maven
Apps Maven

Reputation: 1420

If you will observe the class name of the element, it is the compound class name(having space in between). And compound class names are not permitted in selenium. In this case, you can use another locator like CSS(with class name) or XPath.

Check the following code snippet:

WebDriverManager.chromedriver().version("2.40").setup();
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/test/login.html");
WebElementuserName=driver.findElement(By.cssSelector(".is_required.validate.account_input.form-control"));
userName.sendKeys("Username");
WebElement password = driver.findElement(By.xpath("(//[@class='is_requiredvalidateaccount_input form-control'])[2]"));
password.sendKeys("Password");

Upvotes: 2

K Shashank
K Shashank

Reputation: 29

You can use the class name to find the element as well, But what is the problem is that same class can be used to define multiple element in the program , So when you find element by class it will not assure you that it will find the element which you are hoping for. Selenium will find element of the first in the result.

Better to with xpath like tag along with the class. Here is the code

xpath = " //input[@name='passwd'] ";

@ -> can be used to get any parameter inside the tag and use it as per your requirement.

This will help you. Contact for more information.

Upvotes: 2

Amruta
Amruta

Reputation: 1166

Corrected Code: Xpath of your class name was incorrect.

driver = new ChromeDriver();          
    driver.Url = "http://demo.guru99.com/test/login.html";
    driver.Navigate();
    IWebElement search = driver.FindElement(By.ClassName("is_required validate account_input form-control"));
    search.SendKeys("test");

Another way to access email field is:

IWebElement search = driver.FindElement(By.Xpath("//*[@id='email']"));

Hope this helps.

Upvotes: 1

Related Questions