Mehmet Fatih

Mehmet Fatih

  • 853
  • 931
  • 40.5k

Error in criteria not found when retrieving data based on search

Jan 6 2024 7:17 AM

I am pulling data from the website with Selenium according to the numbers I pulled from Excel to the datagridview. However, if a number cannot be found, the program gives an error and stops working. How can I make it skip numbers it can't find? My code block is as follows. Note: Link lines have been deleted for security purposes. Please ignore it.

drv.SwitchTo().Window(drv.WindowHandles.ToList().Last());

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    var val0 = dataGridView1.Rows[i].Cells[0].Value.ToString();
    drv.FindElement(By.Id("txtTC")).SendKeys(val0);
    Thread.Sleep(3000);
    drv.FindElement(By.Id("btnAra")).Click();
    Thread.Sleep(3000);

    if (val0 != null && val0 != string.Empty)
    {
    var val1 = drv.FindElement(By.Id("txtKisiTCKimlikNo")).GetAttribute("value"); // It gives error when val1 is not found.
    var val2 = drv.FindElement(By.Id("txtAdi")).GetAttribute("value"); 
    var val3 = drv.FindElement(By.Id("txtSoyadi")).GetAttribute("value"); 


    dataGridView1.Rows[i].Cells[1].Value = val1;
    dataGridView1.Rows[i].Cells[2].Value = val2;
    dataGridView1.Rows[i].Cells[3].Value = val3;

    drv.FindElement(By.XPath("/html/body/form[2]/table/tbody/tr[3]/td[1]/div/div/table/tbody/tr[7]/td/table/tbody/tr[2]/td/table[2]/tbody/tr[7]/td")).Click();    Thread.Sleep(3000);
    var val4 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[2]/div/div/div[1]")).Text; 
    Thread.Sleep(3000);
    var val5 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[3]/div/div/div[1]")).Text; 
    Thread.Sleep(3000);
    var val6 = drv.FindElement(By.XPath("//div[9]/div/div/div/input")).GetAttribute("value"); 
    Thread.Sleep(3000);
    var val7 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[2]/div/div[2]/div/div/h4")).Text; 

    dataGridView1.Rows[i].Cells[4].Value = val4;
    dataGridView1.Rows[i].Cells[5].Value = val5;
    dataGridView1.Rows[i].Cells[6].Value = val6;
    dataGridView1.Rows[i].Cells[7].Value = val7;


    drv.FindElement(By.XPath("/html/body/form[2]/nav/div/div/div[2]/ul[2]/li[2]/ul/li[6]/a")).Click(); 
    Thread.Sleep(3000);
    var val8 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[2]/div/div/div[1]")).Text; 
    Thread.Sleep(3000);
    var val9 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[3]/div/div/div[1]")).Text; 
    Thread.Sleep(3000);
    var val10 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[9]/div/div/div/input")).GetAttribute("value"); 
    Thread.Sleep(3000);
    var val11 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[2]/div/div[2]/div/div/h4")).Text;

    dataGridView1.Rows[i].Cells[8].Value = val8;
    dataGridView1.Rows[i].Cells[9].Value = val9;
    dataGridView1.Rows[i].Cells[10].Value = val10;
    dataGridView1.Rows[i].Cells[11].Value = val11;
    }
}

 


Answers (6)