selenium 网络驱动程序 运行 作为不同的用户未获得 profile/session 的用户

selenium web driver running as different user not getting profile/session of user

我有一个奇怪的情况,我稍微修改了 selenium web 驱动程序代码以允许驱动程序服务在不同的用户下启动,对代码的更改 github 是:

public void Start()
    {
        this.driverServiceProcess = new Process();
        if (this.user != null)
        {
            this.driverServiceProcess.StartInfo.UserName = user.Name;
            this.driverServiceProcess.StartInfo.Password = user.Password;
            this.driverServiceProcess.StartInfo.Domain = user.Domain;
        }
        this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.driverServicePath, this.driverServiceExecutableName);
        this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
        this.driverServiceProcess.StartInfo.UseShellExecute = false;
        this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
        this.driverServiceProcess.Start();
        bool serviceAvailable = this.WaitForServiceInitialization();

        if (!serviceAvailable)
        {
            string msg = "Cannot start the driver service on " + this.ServiceUrl;
            throw new WebDriverException(msg);
        }
    }

在实例化 Web 驱动程序的调用中从我的外部代码传入用户详细信息的位置:

new ChromeDriver(userName, password, domain);

new InternetExplorerDriver(ieOptions, userName, password, domain);

并通过传播。

这在所需的用户凭据下成功启动了 chrome 驱动程序,但 IE 出现问题。

此外,chrome 驱动程序与 chrome 以给定用户手动启动(即不通过 selenium 驱动程序)的行为不同。特别是在 NTLM 质询时不会自动传递用户凭据。

我发现,如果我作为所需用户有一个交互式会话 运行(只需从命令行使用 runas /user:<theUser> cmd.exe 并保持会话打开),那么浏览器在通过 selenium 网络驱动程序启动时符合预期,包括自动响应 NTLM 挑战。

如果我在创建 Web 驱动程序之前使用 Process.Start() 以所需用户身份启动 cmd.exe,这将不起作用。

我的问题是:

与从命令行启动进程的交互式会话相比,以编程方式启动进程(使用 Process.Start())有何不同?

有什么方法可以忠实地重现从代码中的命令行启动会话的效果,以便我可以自动化该过程并让我的 Web 驱动程序按照我的意愿执行?

注意:我尝试使用 .net 模拟启动网络驱动程序(按照建议 here and here) rather than modifying the selenium code to run the driver service under another user, but the requests sent from the driver to a server were all sent under my user rather than the one specified by the impersonation (see here

你试过这个设置吗?

this.driverServiceProcess.StartInfo.LoadUserProfile = true;

当前的 Selenium .NET 源代码不再需要此技术。 DriverProcessStarting 事件现在允许用户修改用于启动驱动程序服务进程的 ProcessStartInfo 对象。完成此操作的代码如下所示:

假设您的用户对象看起来像这样:

public class User
{
    public string UserName { get; set; }
    public SecureString Password { get; set; }
    public string Domain { get; set; }
    public bool LoadUserProfile { get; set; }
}

您可以使用如下内容:

public IWebDriver StartInternetExplorerDriver(InternetExplorerOptions options, User user)
{
    InternetExplorerDriverService service = InternetExplorerDriverService.CreateDefaultService();
    service.DriverProcessStarting += (object sender, DriverProcessStartingEventArgs e) =>
    {
        e.DriverServiceProcessStartInfo.UserName = user.UserName;
        e.DriverServiceProcessStartInfo.Password = user.Password;
        e.DriverServiceProcessStartInfo.Domain = user.Domain;
        e.DriverServiceProcessStartInfo.LoadUserProfile = user.LoadUserProfile;
    };

    return new InternetExplorerDriver(service, options);
}