2

enter image description hereErrors Generated On Running CodeError - Unexpected CharacterI'm getting following error at verification stage:

"An unhandled exception of type 'System.NullReferenceException' occurred in nunit.framework.dll

Additional information: Object reference not set to an instance of an object."

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Globalization;
using System.Threading.Tasks;

namespace Login_Val_Test_Do_Not_Use
{
    class Program
    {
        static void Main(string[] args)
        {

            //Instantiate Firefox Driver
            //Go to CV Library Website
            var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://www.cv-library.co.uk/");

            //Wait for web element to become visible
            System.Threading.Thread.Sleep(15000);

            //Click on Login button
            driver.FindElement(By.XPath(".//*[@id='home-nav-right']/a[1]")).Click();

            //Verification that specified page opens
            var loggedInHeader = driver.FindElement(By.Id("cand-login-tabs"));
            Assert.IsTrue(loggedInHeader.Displayed, "Unable to access page");

            }
    }
}
  • Can you please try this instead? driver.FindElement(By.XPath("//*[@id='home-nav-right']/a[1]")).Click(); – Yu Zhang Jul 9 '17 at 6:05
  • 1
    At what line of code are you getting the error. – Alok Jul 9 '17 at 8:15
  • @Yu Zhang - I get an error message on using the XPath. Please, see screen shot. – OA345 Jul 10 '17 at 17:48
  • @ Alok - Assert.IsTrue(loggedInHeader.Displayed, "Unable to access page"); – OA345 Jul 10 '17 at 17:53
1

If you're getting the error on the assertion line, it's exactly what the message tells you - you're getting a null reference. Often this is due to the fact that your FindElement() fails, so then when you try to access one of the methods or properties (e.g. click() or Displayed), you get a null reference error since the object isn't what you think it is (it's null because the element wasn't found).

For debugging, you can just add some checks after each of the FindElement() calls to verify an element was found, but really, the stack trace/console logging should point you to the exact line that failed.

1

The following should work. I've used the class name for the login button instead of using XPath (seems more appropriate here) + always use explicit waits never Thread.sleep!

//Instantiate Firefox Driver
//Go to CV Library Website
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.cv-library.co.uk/");

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement loginButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.ClassName("nav-login")));

loginButton.Click();

//Verification that specified page opens
var loggedInHeader = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("cand-login-tabs")));
Assert.IsTrue(loggedInHeader.Displayed, "Unable to access page");
  • @GMcCannon - Many thanks for your response. But the code generated errors. I have options that I can use to fix it. But, wanted to run it by you 1st.. Many Thanks. – OA345 Jul 10 '17 at 18:08
  • Sorry about that. Fixed a couple of typos but I don't have access to VS right now. If it's still giving you errors, let me know what they are and I'll do my best to figure it out. – NotInventedHere Jul 10 '17 at 23:25
  • Oh just saw you added a screenshot with the errors. The two 'By' errors were caused by the typos I've already fixed. The reason you are getting the 'driver' error is because you've left out the first four lines of code - They were from your original code btw. – NotInventedHere Jul 10 '17 at 23:45
  • Did this work or not? – NotInventedHere Jul 14 '17 at 13:29
  • @ NotInventedHere, apologies for replying rather late. Thanks you very much for the assistance. I get the same error. I have "googled the error". It appears the compiler is not "seeing" the value in question. It appears as not "null" to it ? Here: 1. stackoverflow.com/questions/5120781/… 2. stackoverflow.com/questions/5261250/… I am still researching this issue. I really appreciate your assistance. – OA345 Jul 14 '17 at 23:30

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.