无法使用C#在Selenium上使用已保存的配置文件启动Chrome驱动程序

时间:2017-07-06 10:47:36

标签: c# google-chrome selenium cookies

我写了一个方法来启动具有不同参数的清除Chrome驱动程序,它可以工作。但是当我需要使用我的默认配置文件启动Chrome时(使用已保存的Cookie) - 我编写了这行代码,它运行浏览器(我看到我的书签,因此它运行时使用Cookie),但是visual studio会在一分钟内睡觉返回线,并给我一个例外。这有什么问题?

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);

enter image description here

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

1 个答案:

答案 0 :(得分:0)

您需要引用路径,因为它包含空格字符:

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