Search code examples
c#multithreadingwinformsseleniumgoogle-chrome

Selenium webdriver multiple threads


I have a winforms project that whenever a button is clicked, it calls another class which uses selenium. I am trying to make it so I could run 1 - however many windows I want running. So, I made it so that whenever the button is clicked it makes a new thread and calls a method that calls the other class. The issue is whenever I do multiple windows, it appears that only one window is doing the action. So if I want two windows to try and select a country, only one will select a country. Both windows however, open the url. I am really confused how or why this is happening, I initialize a new webdriver in the constructor so I don't understand why it doesn't seem to do anything else.

using System;
using System.Threading;
using System.Windows.Forms;

namespace MRE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(generate));
            t.IsBackground = true;
            t.Start();
           // if (!InvokeRequired)
           // {
           //     Invoke((Action)generate);
          //  }
        }

        private void generate()
        {
            
            Class1 generator = new Class1();
            generator.start();
        }
    }
}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;

namespace MRE
{
    class Class1
    {
        public static IWebDriver driver;
        public static WebDriverWait wait;


        public Class1()
        {

            driver = new ChromeDriver();
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));


        }

        public void start()
        {
            try
            {
                driver.Navigate().GoToUrl("your url");

                //Input DOB


                SelectElement oSelect = new SelectElement(driver.FindElement(By.Id("capture-country")));
                Thread.Sleep(2000);
                oSelect.SelectByValue("GBR");
                //click
                wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("dob-field-inactive"))).Click();


                //enter Month
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-month"))).SendKeys("01");
                Thread.Sleep(1000);
                //enter Day
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-day"))).SendKeys("01");
                Thread.Sleep(1000);
                //enter Year
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-year"))).SendKeys("2000");


            }

            catch (WebDriverException e)
            {
            }
        }
    }
}

Solution

  • I actually found one way to solve this problem. Instead of initializing the Webdriver as a class variable, I made it so it was a local variable to the start() method. It is not shown in my MRE, but in my actual class I have different methods that use the driver, so I made it so the parameters for any methods I called included IWebDriver as a parameter. Therefore there wasn't one instance of the webdriver running in multiple windows. If there is another way around this please let me know.