简体   繁体   English

Selenium C#-模拟后运行Selenium驱动程序时发生异常

[英]Selenium C# - exception when run selenium driver after impersonate

I am using windows functionality, impersonate, to change the active user before starting Chrome driver. 我正在使用Windows功能来模拟,以在启动Chrome驱动程序之前更改活动用户。 Now, starting the driver without the impersonate code works fine. 现在,在没有模拟代码的情况下启动驱动程序可以正常工作。 Also the impersonate code works fine; 模拟代码也可以正常工作; I see the user is changed. 我看到用户已更改。 But when this change happens and after that I run IWebDriver driver=new ChromeDriver then the exception is triggered on that exact code and the test stops. 但是,当发生此更改时,此后我运行IWebDriver driver=new ChromeDriver则会在该确切代码上触发异常,并且测试将停止。 Any ideas why this happens? 任何想法为什么会发生这种情况?

Here is main part of the code used (the code is just little modified code from another post here at stackoverflow) 这是所用代码的主要部分(该代码只是stackoverflow上另一篇文章的少量修改代码)

namespace localSeleniumTest.Impersonation
{
    class Program
    {
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(stringpszUsername, string pszDomain, string pszPassword,
            int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        // closes open handes returned by LogonUser
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        public void Impersonation()
        {
            WindowsImpersonationContext impersonationContext = null;
            IntPtr userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;
            string domain = Config.domain;
            string user = Config.username;
            string password = Config.password;

            try
            {
                String currentName = WindowsIdentity.GetCurrent().Name;

                // if domain name was blank, assume local machine
                if (domain == "")
                    domain = System.Environment.MachineName;

                // Call LogonUser to get a token for the user
                bool loggedOn = LogonUser(
                    user,
                    domain,
                    password,
                    LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT,
                    ref userHandle
                    );

                if (!loggedOn)
                {
                    return;
                }

                // Begin impersonating the user
                impersonationContext = WindowsIdentity.Impersonate(userHandle);

                String afterImpersonationName = WindowsIdentity.GetCurrent().Name;


                /*this few lines below does not work after impersonation but 
work perfectly without the code above.*/
                    IWebDriver driver = new ChromeDriver();
                    driver.Navigate().GoToUrl("www.google.com");
                    System.Threading.Thread.Sleep(6000);
                    driver.Quit();

Found the issue. 找到了问题。 D user did not have permission to chrome driver i Bin folder D用户没有访问Chrome驱动程序i Bin文件夹的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM