简体   繁体   中英

Can't start Chrome Driver with saved profile on Selenium with C#

I wrote a method to start clear Chrome Driver with different parameters, and it works. But when I was needed to start Chrome with my default profile (with saved cookies) - I wrote this lines of code, it runs browser (and I see my bookmarks, so it runs with cookies), but visual studio sleeps for a minute at return line, and throw me an exception. What is the problem here?

public RemoteWebDriver SetDriver(String Browser)
    {
        switch (Browser)
        {
            case "CH":   
                ChromeOptions options = new ChromeOptions();                    
                options.AddArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
                return new ChromeDriver(options);

在此处输入图片说明

OpenQA.Selenium.WebDriverException was unhandled
HResult=-2146233088
Message=The HTTP request to the remote WebDriver server for URL http://localhost:52036/session timed out after 60 seconds.
Source=WebDriver
StackTrace:
w OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\HttpCommandExecutor.cs:wiersz 170
w OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\HttpCommandExecutor.cs:wiersz 121
w OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\DriverServiceCommandExecutor.cs:wiersz 84
w OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:wiersz 1177
w OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:wiersz 1114
w OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:wiersz 141
w OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options) w c:\Projects\webdriver\dotnet\src\webdriver\Chrome\ChromeDriver.cs:wiersz 76
w AutoLike.Login.SetDriver(String Browser) w d:\users\documents\visual studio 2015\Projects\AutoLike\AutoLike\Login.cs:wiersz 70
w AutoLike.Login..ctor(String Browser, String URL, String DownloadPath) w d:\users\documents\visual studio 2015\Projects\AutoLike\AutoLike\Login.cs:wiersz 32
w AutoLike.Form1.button1_Click(Object sender, EventArgs e) w d:\users\documents\visual studio 2015\Projects\AutoLike\AutoLike\Form1.cs:wiersz 34
w System.Windows.Forms.Control.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
w System.Windows.Forms.Control.WndProc(Message& m)
w System.Windows.Forms.ButtonBase.WndProc(Message& m)
w System.Windows.Forms.Button.WndProc(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.Run(Form mainForm)
w AutoLike.Program.Main() w d:\users\documents\visual studio 2015\Projects\AutoLike\AutoLike\Program.cs:wiersz 19
w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException: 
HResult=-2146233079
Message=Upłynął limit czasu operacji
Source=System
StackTrace:
w System.Net.HttpWebRequest.GetResponse()
w OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request) w c:\Projects\webdriver\dotnet\src\webdriver\Remote\HttpCommandExecutor.cs:wiersz 162

You need to quote the path since it contains a space character:

options.AddArguments(String.Format(
   "--user-data-dir=\"{0}\"", @"C:\Users\user\AppData\Local\Google\Chrome\User Data"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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