Miror browser sesion from one account to another

I have windows forms application which is asking me for username and password in Visual Studio and using Selenium and JavaSCript. When I enter the credentials in Windows Forms I have a script which is opening the web browser and logging in to a website with username and password.
What I want after that to happen is to prompt me again the same form to enter another credentials which will log me into another account without opening the browser and copy the actions which I will do on the first account which is already opened.

using System.Diagnostics;
using System.Security.Policy;
using System.Windows.Forms;

namespace Bet
{
    public partial class Form1 : Form
    {
        private WebBrowser webBrowser1;
        private string url;

        private WebAutomationManager _webAutomation;

        public Form1()
        {
            InitializeComponent();
            _webAutomation = new WebAutomationManager();
                    _webAutomation2 = new WebAutomationManager(); // Initialize the second instance
        }
        public Form1(WebBrowser webBrowser)
        {
            InitializeComponent();
            // Initialize the WebBrowser control
            webBrowser1 = webBrowser ?? throw new ArgumentNullException(nameof(webBrowser));
            Controls.Add(webBrowser1); // Add the control to the form's controls collection
        }
        WebAutomationManager webAutomation = new WebAutomationManager();
        private void button2_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;
            _webAutomation.Login(username, password);


        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Prompt the user to enter credentials for the second account
            string username = textBox1.Text;
            string password = textBox2.Text;
            _webAutomation.Login(username, password);

            // Perform login using web automation with the new credentials
            //webAutomation.Login(secondUsername, secondPassword);

            // TODO: Implement logic to check if the login was successful.
            // If successful, continue with further actions for the second account.

            // Example: Navigate to a page on the website for the second account
            NavigateToWebsite("https://mywebsite.com");
        }

        private string PromptUserForInput(string prompt)
        {
            // Display an input box to prompt the user for input
            string userInput = Microsoft.VisualBasic.Interaction.InputBox(prompt, "User Input", "");

            // Check if the user canceled the input box
            if (string.IsNullOrEmpty(userInput))
            {
                return string.Empty;
            }

            return userInput;
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            _webAutomation.Close(); // Close the WebDriver when the form is closed
            base.OnFormClosing(e);
        }
        private void NavigateToWebsite(string url)
        {
            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = url,
                    UseShellExecute = true
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }
        }
    }
}

button2 is my intial login button which is used to log me in into first account. I tried to create another button, button1 for logging in an calling the form in PromptUserForInput, but with no success.
Thanks

Leave a Comment