I need to make a system that enters a website and enter the cpf, if cpf is registered in this site he can get the client's email and phone, after that impute this data in an excel worksheet.
It takes the cpf's from a worksheet in excel as it shows in the code, but I do not know how to give update in this same spreadsheet inserting the data that I got in the site, could someone help me?
obs: I'm using selenium to get site data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Threading;
using System.Configuration;
using Microsoft.Office.Interop.Excel;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using ClosedXML.Excel;
namespace Teste
{
class Program
{
static void Main(string[] args)
{
try
{
var casos = ImportarCasos();
foreach (var caso in casos)
{
var driver = new ChromeDriver();
driver.Url = "";
driver.Navigate();
Thread.Sleep(2000);
// public bool realizarlogin(string usuario,string senha)
driver.FindElementByXPath("//input[contains(@name, 'Username')]").SendKeys("trh00321");
Thread.Sleep(500);
driver.FindElementByXPath("//input[contains(@name, 'Password')]").SendKeys("MAtteLeao123");
Thread.Sleep(500);
var dominio = driver.FindElements(By.XPath("//select[contains(@name, 'Domain')]"));
dominio[0].Click();
Thread.Sleep(2000);
driver.FindElement(By.XPath("//*[@id='Domain']/option[1]")).Click();
driver.FindElement(By.XPath("//input[contains(@name, 'send')]")).Click();
var abasAbertas = driver.WindowHandles;
while (abasAbertas.Count < 1)
{
abasAbertas = driver.WindowHandles;
Thread.Sleep(1000);
}
driver.SwitchTo().Window(abasAbertas[1]);
driver.FindElement(By.XPath("//img[contains(@src, 'http://icareclientes.sky.com.br/ICareUICommonCDN/Content/images/bt_busca-avancada.png')]")).Click();
Thread.Sleep(2000);
driver.FindElement(By.XPath("//input[contains(@name, 'FiscalCode')]")).SendKeys(caso);
driver.FindElement(By.XPath("//input[contains(@id, 'btnAdvancedSearch')]")).Click();
//contém o cpf no icare
IWebElement codigo = driver.FindElement(By.XPath("//*[@id='cGrid']/table/tbody/tr/td[2]"));
string inserir_codigo = codigo.Text;
//var teste = driver.FindElements(By.XPath("//span[contains(@class, 'ui-icon ui-icon-closethick') and contains(text(), 'close')]"));
var teste = driver.FindElements(By.XPath("//a[contains(@class, 'ui-dialog-titlebar-close ui-corner-all')]/span")).Where(x => x.Size.Height != 0).ToList();
teste[0].Click();
IWebElement codigo1 = driver.FindElement(By.XPath("//input[contains(@name, 'id')]"));
codigo1.Clear();
codigo1.SendKeys(inserir_codigo);
driver.FindElement(By.XPath("//input[contains(@id, 'btnSearch')]")).Click();
}
}
catch (WebDriverException ex)
{
throw;
}
}
public static List<string> ImportarCasos()
{
var listaCPF = new List<string>();
try
{
var path = RetornaNomeArquivo();
string s = "";
Application app = new Application();
app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
Workbook book = app.Workbooks.Open(path);
Worksheet sheet = (Worksheet)book.Worksheets[1];
var xlRange = (Range)sheet.Cells[sheet.Rows.Count, 1];
long lastRow = (long)xlRange.get_End(XlDirection.xlUp).Row;
Range rangeCPF = sheet.get_Range("A2", $"A{lastRow}");
object[,] valueCPF = (object[,])rangeCPF.Value2;
for (int i = 1; i <= valueCPF.GetLength(0); i++)
{
for (int j = 1; j <= valueCPF.GetLength(1); j++)
{
s = valueCPF[i, j].ToString().Replace(".", "").Replace("-","").PadLeft(11);
listaCPF.Add(s);
}
}
return listaCPF;
}
catch (Exception e)
{
return listaCPF;
}
}
public static string RetornaNomeArquivo()
{
string pasta = @"C:\Users\nicolly\Documents\Teste";
DirectoryInfo directory = new DirectoryInfo(pasta);
FileInfo[] files = directory.GetFiles("*.xlsx");
string teste = files[0].ToString();
return Path.Combine(pasta, teste);
}
}
}