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 :)