0

When i run this code i am receiving an error as "Object reference not set to instance of object " its able to click on the first element "Services" but unable to print the text or the a href link. a href link text is "Services"

    public void TestSetUp()
    {
        // the same way we can setup webDriver to use other browsers
        driver = new FirefoxDriver();

        // set the timeout after page load to 30seconds
        driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

    }


    public void TestMethod1()
    {

        driver.Navigate().GoToUrl("http://www.prokarma.com/");

        driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 20));

        IWebElement hover1 = driver.FindElement(By.XPath ( "//*[@id='oe_menu']" ));
        IList<IWebElement> subelements = hover1.FindElements(By.TagName("li"));
        System.Console.WriteLine("Number of elements under li tag are :-->" + subelements.Count);

        for (int i = 0; i < subelements.Count; i++)
        {
            subelements[i].Click();
            string mesage = subelements[i].GetAttribute("a href").ToString();
            Console.WriteLine("Value from textbox is: " + mesage);

        }   

    }

    [TestFixtureTearDown]
    public void FixtureTearDown()
    {

        driver.Quit();
    }
}

}

1
  • 33 li tags are there , under each li , a href texts are services etc etc , i am not able to print the text "Services " under the li tag. Commented Jan 2, 2014 at 6:40

2 Answers 2

2

To get your link from href you need to ask for attribute "href" not "a href":

  string mesage = subelements[i].GetAttribute("href");
Sign up to request clarification or add additional context in comments.

1 Comment

@user3065782, then you should accept this as the answer to your question.
0

Your null pointer is comming because th4e below value is returning null. Your syntax is wrong here

subelements[i].GetAttribute("a href") 

You need to change this line to and then it should work

subelements[i].GetAttribute("href") 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.