gpt4 book ai didi

c# - 为自动化任务运行多个 chrome(配置文件)实例

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:32 29 4
gpt4 key购买 nike

我想运行多个具有不同配置文件的 chrome 实例(每个配置文件都有它们的 cookie)以同时执行某些任务。例如,我想使用 2 个帐户在 Google 上进行搜索(每个帐户都有自己的代理)。

我使用 Visual Studio Community 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");
}
}
}

运行此代码时,打开每个配置文件,什么都不做。

有什么帮助吗?

最佳答案

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

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

关于c# - 为自动化任务运行多个 chrome(配置文件)实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727328/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com