public async Task Start(string uriStr, Operation operation, Script script = null) { await Task.Run(() => { Uri uri = new Uri(uriStr); // 触发开启事件 if (this.OnStart != null) { this.OnStart(this, new OnStartEventArgs(uri)); } // 实例化PhantomJS的WebDricer PhantomJSDriver driver = new PhantomJSDriver(_service); try { DateTime watch = DateTime.Now; // 请求Url driver.Navigate().GoToUrl(uriStr); // 执行JavaScript代码 if (script != null) { driver.ExecuteAsyncScript(script.Code, script.Args); } // 执行网页操作 if (operation.Action != null) { operation.Action.Invoke(driver); } // 设置超时时间 WebDriverWait driverWatch = new WebDriverWait(driver, TimeSpan.FromMilliseconds(operation.timeout)); if (operation.Condition != null) { driverWatch.Until(operation.Condition); } // 获取线程ID int threadId = Thread.CurrentThread.ManagedThreadId; // 获取执行时间 long milliseconds = DateTime.Now.Subtract(watch).Milliseconds; string pageSource = driver.PageSource; // 触发结束事件 if (this.OnCompleted != null) { this.OnCompleted(this, new OnCompletedEventArgs(uri, threadId, pageSource, driver, milliseconds)); } } catch (Exception e) { if (this.OnError != null) { this.OnError(this, e); } } finally { driver.Close(); driver.Quit(); } }); }