0

actualmente quiero cerrar el navegador despues de ejecutar una rutina diaria, llevo días intentandolo, incluso con selenium pero por cuestiones de versiones entre selenium y los navegadores no me permite usarlo, por lo tanto ya no uso selenium

Estuve trabajando en este código pero al final no me cierra el navegador, se queda abierto, espero puedan ayudarme, saludos!

    public static void Main(string[] args)
    {
        string[] uri = new string [5] { "http://www.google.com", "http://www.yahoo.com", "http://www.linkedin.com", "http://www.stackoverflow.com", "http://www.microsoft.com" };
        int urlCurrentIndex = 4;
        Console.WriteLine("Welcome to the Daily Routine\n");
        while (urlCurrentIndex < 5)
        {
            foreach (string i in uri){
                var exe = new ProcessStartInfo("explorer.exe");
                exe.Arguments = i;
                Process.Start(exe);
                var process = Process.GetCurrentProcess();

                if (process != null)
                {
                    Console.WriteLine("Routine in progress...");
                    Console.WriteLine(i);
                    Thread.Sleep(1000);
                    process.Close();
                    urlCurrentIndex++;
                    
                    if (urlCurrentIndex <= 4)
                    {
                        Console.WriteLine("Routine has been completed.");
                        process.Kill();
                    }
                }
                else
                {
                    Console.WriteLine("Routine failed.");
                }

            }
        }
6
  • 1
    Hola, bienvenidx a SOes. Te muestra algún error?
    – Alfabravo
    el 7 ene. 2022 a las 22:06
  • No veo que estés usando selenium.
    – Mateo
    el 7 ene. 2022 a las 23:35
  • Le muestra el mensaje de : Console.WriteLine("Daily Routine has been completed."); ????? el 10 ene. 2022 a las 17:16
  • hace falta el navegador completo? tal vez un webclient sea suficiente el 10 ene. 2022 a las 18:05
  • Y si mejor haces una llamada http usando HttpClient desde c#, asi obtendras una respuesta y no necesitas abrir ningun navegador. el 10 ene. 2022 a las 18:44

1 respuesta 1

0

Logre solucionar el problema, sé que no es la mejor opción pero funciona, se los comparto, se que a alguien le puede servir

static void Main(string[] args)
    { "http://www.google.com", "http://www.yahoo.com", "http://www.linkedin.com", "http://www.stackoverflow.com", "http://www.microsoft.com" };

        int urlCurrentIndex = 4;
        Console.WriteLine("Daily Routine is running...");
        Console.WriteLine("Please, don't close Terminal\n");
        while (urlCurrentIndex < 5)
        {
            foreach (string i in uri)
            {
                var exe = new ProcessStartInfo("explorer.exe");
                exe.Arguments = i;
                Process.Start(exe);
                var process = Process.GetCurrentProcess();

                if (process != null)
                {
                    Console.WriteLine("Routine in progress...");
                    Console.WriteLine(i);
                    Thread.Sleep(1000);
                    process.Close();
                    urlCurrentIndex++;
                }
                else
                {
                    Console.WriteLine("Routine failed.");
                }
            }

Se agrega esta sección de código para cerrar los procesos de los navegadores elegidos

            Thread.Sleep(30000);
            Process[] AllProcesses = Process.GetProcesses();
            foreach (var process in AllProcesses)
            {
                if (process.MainWindowTitle != "")
                {
                    string s = process.ProcessName.ToLower();
                    if (s == "iexplore" || s == "iexplorer" || s == "chrome" || s == "firefox" || s == "msedge")
                        process.Kill();
                }
            }
            Console.WriteLine("Routine has been finished successfully!");
            Thread.Sleep(3000);
        }
    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.