0

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
        System.Threading.Thread.Sleep(15000);

        //Click on Table button
        driver.FindElement(By.Id("detail-table-view")).Click();

        //Obtaining pressure For 2100 hours today
        //driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[6]")).Text;
        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
        driver.FindElement(By.XPath(".//*[@id='blq-content']/div[7]/div[2]/ul/li[3]/a/div/h3/span")).Click();

        //Obtain pressure for 2100 Hours tomorrow
        //driver.FindElement(By.XPath(".//*[@id='hourly']/div[3]/table/tfoot/tr[3]/td[16]")).Text;
        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);
    }
}
1

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.