小编典典

seleniumWeb驱动程序以其他用户身份运行而未获取用户的个人资料/会话

selenium

我有一个奇怪的情况,我已对SeleniumWeb驱动程序代码进行了一些修改,以允许在其他用户下启动驱动程序服务,对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从命令行使用并保持会话打开),则通过seleniumWeb驱动程序启动时,浏览器的所有功能都是预期的,包括自动启动。应对NTLM挑战。

如果我Process.Start()在创建Web驱动程序之前以所需用户身份启动cmd.exe,则此操作将无效。

我的问题是这样的:

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

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

_注意:我尝试使用.net模拟启动Webdriver,而不是修改selenium代码以在另一个用户下运行驱动程序服务,但是从驱动程序发送到服务器的请求都在我的用户下发送比模仿所指定的


阅读 377

收藏
2020-06-26

共1个答案

小编典典

当前的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);
}
2020-06-26