1

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

  • 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 Jan 26 '17 at 2:38
  • I used XPath & I got the right Id... Sorted.. Thank you. – OA345 Jan 27 '17 at 11:51
2

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();
| improve this answer | |
  • Thank you very much for your response. This is the error message I get when I run your code: Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'WebElement' could not be found (are you missing a using directive or an assembly reference?) StarWood_Hotels_Valid_User_Login c:\users\xxxx\onedrive\documents\visual studio 2015\Projects\StarWood_Hotels_Valid_User_Login\StarWood_Hotels_Valid_User_Login\Program.cs 20 Active – OA345 Jan 26 '17 at 20:46
  • This error is during compiling time? You'll have to import the WebElement library. In java is import org.openqa.selenium.WebElement; In your case may be using OpenQA.Selenium.WebElement. – Thiago Fioravante Jan 26 '17 at 21:00
  • Merci beaucoup.... Yes, the error ocurred during compiling (run) time. I tried importing Selenium.Webelement. But I was not able to locate it. – OA345 Jan 26 '17 at 21:50
  • Are you using C#? The correct is to use IWebElement instead WebElement. Please remove the using OpenQA.Selenium.WebElement – Thiago Fioravante Jan 27 '17 at 11:41
0

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();
| improve this answer | |
  • Thank you so much... The 1st code produced the error: The Type or Namespace 'WebDriverWait' name could not be found (are you missing a using directive or an assembly reference ? Many thanks once again. – OA345 Jan 26 '17 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.