0

I am trying to make method where I could scrap data from webpage that's behind password locked form. I used Selenium Web Driver to successfully connect to website and login there, but I would like to store the data do Dicitonary that I made.

// Custom params

public class Params_Rezervation
{
    public string Time { get; set; } 
    public string Guests { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Status { get; set; } 
    public string Text { get; set; } 
    public string Table { get; set; } 
}

This is the custom params I would like to have in my dicitonary so I could access them. I tried to make this dictionary.

public static Dictionary<int, Params_Rezervace> rezervace_data = new Dictionary<int, Params_Rezervace>();

Then I tried it populate it with data from website, but I fail on adding data to dictionary.

public static void LoadReservation()
    {
        // Setting up a 
        var chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments("--headless");

        // Console hiding
        var chromeDriverService = ChromeDriverService.CreateDefaultService();
        chromeDriverService.HideCommandPromptWindow = true;

        try
        {
            using (var browser = new ChromeDriver(chromeDriverService, chromeOptions))
            {
                // Setting up a website 
                browser.Navigate().GoToUrl("specific website");

                // Entering password + submiting it
                browser.FindElement(By.Id("pwbox-580")).SendKeys("password");
                browser.FindElement(By.Name("Submit")).Click();

                // Finding the specific table
                IWebElement tableElement = browser.FindElement(By.XPath("//*[@id=\"post-580\"]/div/div[2]/table"));

                // Finding the row
                IList<IWebElement> rows = tableElement.FindElements(By.XPath(".//tr"));

                // Data
                int number = 0;

                foreach (IWebElement row in rows)
                {
                    // Finding specific columns
                    IList<IWebElement> columns = row.FindElements(By.XPath(".//th"));

                    //The error line where I dont know how to continue 
                    rezervace_data.Add(number, Params_Rezervace);

                    // Data populating
                    rezervace_data[number].Time = columns[0].Text;
                    rezervace_data[number].Guests = columns[1].Text;
                    rezervace_data[number].Name = columns[2].Text;
                    rezervace_data[number].Email = columns[3].Text;
                    rezervace_data[number].Text = columns[4].Text;
                    rezervace_data[number].Table = columns[5].Text;

                    number++;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error while loading data! \n" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Error I get: System.NullReferenceException: 'The object reference is not set to an instance of the object.'

System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get returned null.

I trying to learn programming so If someone will answer me what to change it will be very helpfull, because I am learning it by myself and websites :)

TriXcze
  • 25
  • 5
  • Pleas don't let us guess what the error is, and rather [edit] your question and clearly state the actual error message and exception details. – Ondrej Tucny Feb 19 '23 at 15:06

1 Answers1

0

Your “error line” should be something like this:

rezervace_data.Add(number, new Params_Rezervace());

You need to add an object in your dictionary of type Params_Rezervace, and the way you do that is with the new keyword.

ZikosFF
  • 44
  • 4