为自动化任务运行多个chrome(概要文件)实例

本文关键字:文件 实例 chrome 任务 自动化 运行 | 更新日期: 2023-09-27 18:30:01

我想用不同的配置文件运行多个chrome实例(每个配置文件都有自己的cookie)来同时执行一些任务。例如,我想在谷歌上搜索2个帐户(每个帐户都有自己的代理)。

我使用Visual Studio社区2015。

这就是我现在所做的(没有代理):

namespace ChromeBot
{
    class Program
    {
        public static object Application { get; private set; }
        static void Main(string[] args)
        {
            //Set specific profile for Google Chrome
            var options = new ChromeOptions();
            options.AddArguments("user-data-dir=C:/Users/conta/AppData/Local/Google/Chrome/User Data/");
            options.AddArguments("--start-maximized");
            options.AddArguments("--profile-directory=Profile 1");

            //Create the reference for our browser 
            IWebDriver driver = new ChromeDriver(options);
            driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 0, 12));

            //Navigate to Google Page
            driver.Navigate().GoToUrl("http://www.google.com");

            //Find the element
            IWebElement element = driver.FindElement(By.Name("q"));

            //Perform ops
            element.SendKeys("cars");

            // Set specific profile for Google Chrome1
            var options1 = new ChromeOptions();
            options1.AddArguments("user-data-dir=C:/Users/conta/AppData/Local/Google/Chrome/User Data/");
            options1.AddArguments("--start-maximized");
            options1.AddArguments("--profile-directory=Profile 2");

            //Create the reference for our browser 1
            IWebDriver driver1 = new ChromeDriver(options1);
            driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 0, 12));
            //Navigate to Google Page 1
            driver1.Navigate().GoToUrl("http://www.google.com");
            //Find the element
            IWebElement element = driver.FindElement(By.Name("q"));

            //Perform ops
            element.SendKeys("smartphones");
        }
    }
}

运行此代码时,打开每个配置文件,不执行任何操作。。

有什么帮助吗?

为自动化任务运行多个chrome(概要文件)实例

当用户通过chromedriver启动chrome时,它会打开chrome浏览器的新实例并锁定用户数据目录。因此,如果任何其他实例试图使用相同的用户数据目录打开,则第二个实例不会响应。

请使用不同的用户数据目录启动每个chrome实例。