简体   繁体   中英

How can I open multiple browser instances in selenium in c#?

I want my program to open multiple browser instances at once and perform actions. But I ran into a problem, which is that all actions are performed in turn. I have a list of proxy servers and data from them, how do I make several browser instances open at once and they start working?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System.Threading;
using OpenQA.Selenium.Chrome.ChromeDriverExtensions;
using System.IO;

namespace LoveStackOverflow
{
    public partial class Form1 : Form
    {
//        IWebDriver Browser;

        public string fileProxyName = @"";
        public int MaxViews;
        public string razdelitel;
        public List<string> ProxyIp = new List<string>();
        public List<int> ProxyPort = new List<int>();
        public List<string> ProxyLogin = new List<string>();
        public List<string> ProxyPassword  =new List<string>();


        public Form1()
        {
            InitializeComponent();
            LabelMaxViewText();
            openFileDialog1.Filter = "TextDoc | *.txt";
            

        }

        private void button1_Click(object sender, EventArgs e)
        {


            LoadBrauzer();

          

        }


        public void LoadBrauzer()
        {

            for (int i = 0; 0 < MaxViews; i++)
            {



                IWebDriver Browser = new ChromeDriver();


                ChromeOptions options = new ChromeOptions();
                options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0");
                //       options.AddArgument("ignore-certificate-errors");
                options.AddArguments("--disable-blink-features");
                options.AddArgument("--disable-blink-features=AutomationControlled");
                options.AddExcludedArgument("enable-automation");
                options.AddArguments("--disable-infobars");
                options.AddHttpProxy(ProxyIp[i], ProxyPort[i], ProxyPassword[i], ProxyLogin[i]);
                //  options.AddArguments("headless"); // hide

                Browser = new OpenQA.Selenium.Chrome.ChromeDriver(options);
                Browser.Manage().Window.Maximize();
                Browser.Navigate().GoToUrl("https://MESITE.ru");
                Browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                CheckAcceptOption( Browser);
                IWebElement FindTextBoxSearch = Browser.FindElement(By.ClassName("char-header-search-module__input"));
                FindTextBoxSearch.SendKeys(textBoxFindVideo.Text + OpenQA.Selenium.Keys.Enter);
                OpenVideo(Browser);
            }
        }


        private void CheckAcceptOption(IWebDriver Browser)
        {
            Actions actionProvider = new Actions(Browser);
            IWebElement FindOption = Browser.FindElement(By.CssSelector(".char-base-button-module__button.char-base-button-module__contained-accent.char-base-button-module__pointerCursor.char-base-button-module__regular"));
            if (FindOption != null)
            {
                actionProvider.Click(FindOption).Perform();
            }
            else return;
        }


        private void OpenVideo(IWebDriver Browser)
        {
            Actions actionProvider = new Actions(Browser);
            IWebElement FindVideo = Browser.FindElement(By.ClassName("pen-h-card-inline__image-wrapper"));
            actionProvider.Click(FindVideo).Perform();

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void LabelMaxViewText()
        {
            LabelMax.Text = "Max = " + MaxViews.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileProxyName = openFileDialog1.FileName;
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string[] split;
            StreamReader Filen = new StreamReader(fileProxyName);
            while ((razdelitel = Filen.ReadLine()) != null)
            {
               
                split = razdelitel.Split(':');
                var timeIpProxy = split[0];
                var timePortProxy = split[1];
                var timeProxyLogin = split[2];
                var timeProxyPassword = split[3];
                ProxyIp.Add(timeIpProxy);
                int x = Int32.Parse(timePortProxy);
                ProxyPort.Add(x);
                ProxyLogin.Add(timeProxyLogin);
                ProxyPassword.Add(timeProxyPassword);
                MaxViews++;
            }
            LabelMaxViewText();
            Filen.Close();
        }
    }
}

I've heard about selenium grid. There must be a way to open as many tabs as I have a proxy, right? I'm trying to implement this through for. My list contains proxy data and it is convenient to take them in a loop, and if you run everything at once, how do I track changes? I don't understand....

What I did for this issue was create a WinForm to have a UI for running my tests. Then at the click of a button id call a function that ran my test. If you did it like I did(see below), itll open an instance of the test for every click(or you can put it in a loop to open as many as needed). The key is to not make the task run async or itll wait for one test to finish before opening another. This may not be exactly what you need, but this is how I was able to accomplish it. Shouldnt need proxies or multiple ports or anything.

        private void Upload_Click(object sender, EventArgs e)
        {
            Task.Run(() => TestFunction(Param1, Param2, Param3));
        }

Example Function

        public void TestFunction(Param1, Param2, Param3)
        {
            //Function to Open Browser, You can call your browser however youd like
            IWebDriver driver = InitBrowser("chrome");
            string LandingPage = "https://google.com";
            driver.Url = LandingPage;

            driver.Quit();
        }

You can see how it opens multiple instances in the image below

这是我运行的测试如何发挥作用的图像

The other answer didn't work for me unfortunately.Based on an answer to a similar question, I was able to configure parallel run of Selenium instances like this:

List<Task> tasks = new();
foreach (var someObject in listOfObjects)
{
    tasks.Add(Class.SomeMethod(arg1, arg2, arg3));
}
await Task.WhenAll(tasks);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM