Error - The Name 'CultureInfo' Does Not Exist In The Current ContextError - An Unhandled Exception Of type 'System.FormatException' Occurredl In Mscorlib.dll

I get this error message when I run this code:

This is my code:

        //Wait for web element to become visible

        //Click on Table button

        //Obtaining pressure For 2100 hours today
        string pressureToday = driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[16]")).Text;

        //Click On next day button on website, i.e. Wednesday 24/05/2017

        //Obtain pressure for 2100 Hours tomorrow
        string pressureTomorrow = driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[15]")).Text;

        //Subtract the two values above and then 'echo' the result in Selenium
        int val1 = Int32.Parse(driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[15]")).Text);
        int val2 = Int32.Parse(driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[16]")).Text);

        int difference = val1 - val2;
      System.Console.WriteLine("Difference is: " + difference);

Probably the text you get contains , or . and you cant convert that to whole number directly. And if you already got the text, there is no need to find it again.

Try it like this:

var _pressureToday = Double.Parse(pressureToday,CultureInfo.InvariantCulture)
var _pressureTomorrow = Double.Parse(pressureTomorrow,CultureInfo.InvariantCulture)

var difference = _pressureToday - _pressureTomorrow;
  • I put that code in & I get an error message: "The name 'CultureInfo' does not exist in the current context." My research led me to this page: stackoverflow.com/questions/9760237/… I have also added a screen shot of the error message. Thanks.
    – OA345
    May 23 '17 at 10:08
  • You can mouseover the problem, and Visual Studio will auto suggest how to solve it, just need to use System.Globalization;
    – George
    May 23 '17 at 10:32

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.