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

I have checked Google for this error. But the solution has not been very helpful. I would appreciate your help, please.

The URL of the webpage that I am trying to automate is: https://www.starwoodhotels.com/preferredguest/account/sign_in.html

The code that I have written up to automate the login page is:

using System;

using System.Collections.Generic; using System.Linq; using System.Text; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using System.Threading.Tasks;

namespace StarWood_Hotels_Valid_User_Login { class Program { static void Main(string[] args) { //Instantiate Firefox Driver var driver = new FirefoxDriver(); driver.Navigate().GoToUrl("https://www.starwoodhotels.com/preferredguest/account/sign_in.html");

        //Enter User Name - AutoTest1
        var user = driver.FindElement(By.Id("userName"));
        user.SendKeys("AutoTest1");

        //Enter Password - Testing123
        var pass = driver.FindElement(By.Id("userPassword"));
        pass.SendKeys("Testing123");

        //Click on submit button 
        driver.FindElement(By.Id("actionLink right")).Click();

    }
}

}

=======================================================================

I have used HTML elements from Firebug to identify the necessary web elements.

========================================================================

Here is the error:

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll

Additional information: Unable to locate element: #userName

share
    
You have got the id's all wrong. first learn how to get the id's correctly from the html code. you have used the classname(userName & userPassword) in place of id's (login & password) – Alok 6 hours ago

Well, I did an inspect on the page and I think that you're looking for the wrong element. The html is (I removed the unnecessary code):

<div class="userName">
   <input tabindex="1" name="login" id="login" type="text" size="12" maxlength="70" value="" class="logintextbox textField validate " data-validationrules="isRequired,omnitureTrackingFrontEnd" placeholder="Username or SPG number">
</div<>
...
<div class="userPassword">
  <input name="password" tabindex="2" id="password" type="password" size="12" maxlength="128" value="" data-validationrules="isRequired,omnitureTrackingFrontEnd" class="logintextbox textField validate " placeholder="Password">
</div>

You should look for the input to send keys and not the div. And you're using By.Id() and the correct is By.className in this case. And I'm not sure if the var user = ... is correct too since I always use WebElement user = ... .

I suggest you to try the code below:

//Enter User Name - AutoTest1
WebElement user = driver.FindElement(By.Id("login"));
user.SendKeys("AutoTest1");

//Enter Password - Testing123
WebElement pass = driver.FindElement(By.Id("password"));
pass.SendKeys("Testing123");

//Click on submit button
driver.FindElement(By.Id("checkSubmit")).Click();
share|improve this answer

Firstly I think you have taken the wrong id for username, password, signIn. as suggested by @Thiago. Update your code with the right ids.

Secondly, "NoSuchElementException" happens when the driver is not able you find the webelement. In your case , as soon as you are navigating to the page. You are trying to enter the username and password. I would suggest go to the page, wait for the webelements to get loaded, then enter the username and password.You can try the below code.

//Initialise to WebDriverWait to wait for 30 seconds before throwing the "NoSuchElementException"
WebDriverWait wait = new WebDriverWait(driver , TimeSpan.FromSeconds(30));

//This will wait for the WebElement 'user' till it is loaded and visible.
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("login")));

Then you can enter the username , password and click on SignIn button

//Enter User Name - AutoTest1
WebElement user = driver.FindElement(By.Id("login"));
user.SendKeys("AutoTest1");

//Enter Password - Testing123
WebElement pass = driver.FindElement(By.Id("password"));
pass.SendKeys("Testing123");

//Click on submit button
driver.FindElement(By.Id("checkSubmit")).Click();
share|improve this answer

Your Answer

 
discard

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.