簡體   English   中英

如何在 c# 中的 selenium 中打開多個瀏覽器實例?

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

我希望我的程序一次打開多個瀏覽器實例並執行操作。 但是我遇到了一個問題,就是所有的動作都是輪流執行的。 我有一個代理服務器列表和來自它們的數據,如何讓多個瀏覽器實例同時打開並開始工作?

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();
        }
    }
}

我聽說過 selenium 網格。 必須有一種方法可以打開與我有代理一樣多的選項卡,對嗎? 我正在嘗試通過 for 來實現這一點。 我的列表包含代理數據,循環使用它們很方便,如果一次運行所有內容,我如何跟蹤更改? 我不明白....

我為這個問題所做的是創建一個 WinForm 來擁有一個用於運行我的測試的 UI。 然后單擊按鈕 id 調用運行我的測試的 function。 如果你像我一樣做(見下文),它會為每次點擊打開一個測試實例(或者你可以將它放在一個循環中以根據需要打開多個)。 關鍵是不要讓任務異步運行,否則它會等待一個測試完成后再打開另一個測試。 這可能不是您所需要的,但這就是我能夠完成它的方式。 不應該需要代理或多個端口或任何東西。

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

示例 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();
        }

您可以在下圖中看到它如何打開多個實例

這是我運行的測試如何發揮作用的圖像

不幸的是,另一個答案對我不起作用。基於對類似問題的回答,我能夠像這樣配置 Selenium 實例的並行運行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM