一、全局配置

options.AddArgument("--no-sandbox");//不在沙盒中运行
options.AddArgument("--headless");//浏览器不提供可视化页面,桌面和状态栏都不会浏览器窗口,静默模式
options.AddArgument("--disable-dev-shm-usage");//克服资源有限的问题
options.AddArgument("--disable-images");//过滤掉图片加载,使页面加载更快
//关闭浏览器头部提示的正在被控制
options.AddExcludedArgument("enable-automation");
options.AddAdditionalCapability("useAutomationExtension", false);
//设置异步加载,异步加载就是不等页面加载完毕,只要页面上出现你所要的值,哪怕页面只渲染了20%,会马上拿到你指定的值,然后就可以进行下一个页面的爬去,速度惊人的快。如果不设置异步加载,经常是因为页面一个广告或图片或JS未加载完毕页面会一直转一直不会去拿你所要的值,而且很多时候还会超时,眼看着页面上有值但是就是拿不到的那种。
options.PageLoadStrategy = PageLoadStrategy.None;

二、页面打开时执行一段JS达到搞事情的目的。

using (driver = new OpenQA.Selenium.Chrome.ChromeDriver(options)){
((OpenQA.Selenium.Chrome.ChromeDriver)driver).ExecuteChromeCommand("Page.addScriptToEvaluateOnNewDocument", new Dictionary<string, object> { { "source", @"Object.defineProperty(navigator, 'webdriver', {get: () => false})" } });。。。。。
}

这样子去设置js脚本会在页面的JS执行之前优先执行我们的JS,好多反爬虫的网站经常会进行一些检测,我们可以根据分析去做一些处理,使其检测失效。

三、使用代理

在chrome里面使用带用户名和密码的代理,需要使用插件去实现,看一下google插件的文档,其实还是挺好实现的。给一段生成插件的代码:

/// <summary>
/// 生成插件
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="userName"></param>
/// <param name="passWord"></param>
public void GeneratePlug(string ip="",string port="",string userName="",string passWord="") {try{string FileName = Path.GetDirectoryName(Application.ExecutablePath) + "\\proxy.zip";string BackGroundJs = @"var config = {mode: 'fixed_servers',rules: {singleProxy: {scheme: 'http',host: 'mimvp_proxy_host',port: parseInt(mimvp_proxy_port)},bypassList: ['{0}']}};chrome.proxy.settings.set({value: config, scope: 'regular'}, function() {});function callbackFn(details) {return {authCredentials: {username: 'mimvp_username',password: 'mimvp_password'}};}chrome.webRequest.onAuthRequired.addListener(callbackFn,{urls: ['<all_urls>']},['blocking']);".Replace("{0}", string.Join(",", Sites).Replace(",", "','"));string ManiFest = "{\"version\": \"1.0.0\",\"manifest_version\": 2,\"name\": \"Chrome Proxy\",\"permissions\": [\"proxy\",\"tabs\",\"unlimitedStorage\",\"storage\",\"<all_urls>\",\"webRequest\",\"webRequestBlocking\"],\"background\": {\"scripts\": [\"background.js\"]},\"minimum_chrome_version\":\"22.0.0\"}";BackGroundJs =BackGroundJs.Replace("mimvp_proxy_host", ip).Replace("mimvp_proxy_port", port).Replace("mimvp_username", userName).Replace("mimvp_password", passWord);List<MemoryStream> ms = new List<MemoryStream> {new MemoryStream(System.Text.Encoding.UTF8.GetBytes(BackGroundJs)),new MemoryStream(System.Text.Encoding.UTF8.GetBytes(ManiFest))};List<string> lt = new List<string> { "background.js", "manifest.json" };Compress(ms, lt, FileName);}catch (Exception ex) { }
}

插件生成完毕后需要

options.AddExtension(FileName);

这样的方式进行设置,FileName就是插件的文件地址。

完整的主程代码:

        public void Process(){//ChromeDriver检查更新UpdateChromeDriver();IWebDriver driver = null;try{CloseWindow();//代理初始化DataTable dt = DbHelperSQL_SR.GetDataTable("select t.ip,t.port,t.username,t.password from tproxy t WHERE t.status=1");if (dt != null && dt.Rows.Count > 0){foreach (DataRow dr in dt.Rows){Agents.Add(new Agent{Ip = dr["ip"].ToString(),Port = dr["port"].ToString(),UserName = dr["username"].ToString(),PassWord = dr["password"].ToString()});}}//物流初始化Dictionary<string, Func<TrackingEntity, IWebDriver, Tracking_Chrome.TrackingRet>> dic = new Dictionary<string, Func<TrackingEntity, IWebDriver, TrackingRet>> {{ "DPDUKN:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPD-red-EUR By Road:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPD-red-Two-Day.co:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPD-GB-A32-手机:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPD-red-Two-Day-手机:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPD-EU-A32-手机:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "DPDUK2:dpd.co,dpd.co.uk",(a,b)=>DPD_CO(a,b)},{ "ES-PE-2:correosexpress.com",(a,b)=>ES_PE_2(a,b)},{ "PL-DPD境外:dpd.de",(a,b)=>EU_DPD(a,b)},{ "EU_DPD:dpd.de",(a,b)=>EU_DPD(a,b)},{ "PL-DHL:51tracking.com",(a,b)=>Tracking51(a,b,"dhl-germany-tracking")},{ "DHLDE:51tracking.com",(a,b)=>Tracking51(a,b,"dhl-germany-tracking")},{ "DEGLS_EU:51tracking.com",(a,b)=>Tracking51(a,b,"gls-tracking")},{ "DEDHL:51tracking.com",(a,b)=>Tracking51(a,b,"dhl-germany-tracking")},{ "PL-DHL境外:51tracking.com",(a,b)=>Tracking51(a,b,"dhl-germany-tracking")},{ "DHL Paket:51tracking.com",(a,b)=>Tracking51(a,b,"dhl-germany-tracking")},{ "ES-SEUR-OVERSEA:seur.com",(a,b)=>SEUR(a,b)},{ "橙联:51tracking.com",(a,b)=>Tracking51(a,b,"orangeconnex-tracking")}};//站点初始化Sites = dic.Keys.ToList().Map(a =>{return a.Split(':')[1];}).Distinct().ToList();List<string> lt_shipping_site = dic.Keys.ToList();WebAgentList = Sites.Map<string, WebAgent>(a =>{return new WebAgent{Site = a,AgentList = Agents.Map(b =>{return new Agent{Ip = b.Ip,Port = b.Port,UserName = b.UserName,PassWord = b.PassWord,Available = 1};}).ToList()};}).ToList();//获取数据 生成数据列表string Sql = string.Format(@"SELECTa.ID,a.Oid,a.TrackingNumber,a.ShippingServer,a.TrackingStatus,a.TrackingDate,a.PaidDate,a.PostalCodeFROM tEBay_Trade aWHERE  a.IsEffective = 1 AND a.PaidDate > dateadd(DAY, -30, getdate()) AND isnull(TrackingNumber, '') <> ''AND ((isnull(a.TrackingStatus, '') not in ('Delivered','派送异常','包裹退回') and isnull(a.TrackingStatus, '') not like '%Delivered%') or (isnull(a.TrackingStatus,'') in('派送异常','包裹退回') and datediff(day,a.TrackingDate,getdate())<=10))AND (a.TrackingDate IS NULL OR datediff(HOUR, a.TrackingDate, getdate()) >= 4)AND a.ShippingServer in('{0}') order by a.id desc", string.Join("','", dic.Keys.ToList().Map(a => { return a.Split(':')[0]; })));List<TrackingEntity> lt = ConverToListHelper<TrackingEntity>.ConvertToList(DbHelperSQL_SR.ExecuteDatatable(Sql));int Total = lt.Count;this.Invoke(new Action(() =>{toolStripStatusLabel1.Text = string.Format("总数:{0}", Total);}));//数据按站点排序lt.Each(a =>{string Site = lt_shipping_site.Find(b => b.Split(':')[0] == a.ShippingServer.Trim()).Split(':')[1];a.Site = Site;});lt.OrderBy(a => a.Site);//根据数据列表依次抓取页面数据List<TrackingRet> Rets = new List<TrackingRet>();OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();string UserDataDir = System.Configuration.ConfigurationSettings.AppSettings["UserDataDir"].ToString();options.AddArgument(string.Format("--user-data-dir={0}", UserDataDir));options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");//options.AddArgument("disable-extensions");options.AddExcludedArgument("enable-automation");options.AddAdditionalCapability("useAutomationExtension", false);//设置异步加载options.PageLoadStrategy = PageLoadStrategy.None;//根据站点一次处理数据List<string> sites_temp = lt.Map(aa => { return aa.Site; }).Distinct().ToList();foreach (string si in sites_temp){IsChangeAgent = false;List<TrackingEntity> lt_data_site = lt.Filter(c => c.Site == si).ToList();int Ct = lt_data_site.Count / (Agents.Count + 1);if (Ct == 0){Ct = 1;}int CurQty = 0;string FileName = Path.GetDirectoryName(Application.ExecutablePath) + "\\proxy.zip";change: if (IsChangeAgent){//切换代理ChangeSite(si);options = new OpenQA.Selenium.Chrome.ChromeOptions();options.AddArgument(string.Format("--user-data-dir={0}", UserDataDir));options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");options.AddExcludedArgument("enable-automation");options.AddAdditionalCapability("useAutomationExtension", false);options.AddExtension(FileName);}else{options = new OpenQA.Selenium.Chrome.ChromeOptions();options.AddArgument(string.Format("--user-data-dir={0}", UserDataDir));options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");options.AddArgument("disable-extensions");options.AddExcludedArgument("enable-automation");options.AddAdditionalCapability("useAutomationExtension", false);this.Invoke(new Action(() =>{this.Text = FormTitle + "         当前未使用代理";}));if (File.Exists(FileName)){try{File.Delete(FileName);}catch { }}}//设置异步加载options.PageLoadStrategy = PageLoadStrategy.None;int PageIndex = 0;List<Agent> plt = WebAgentList.Find(wf => wf.Site == si).AgentList.Filter(af => af.Available == 0).ToList();if (plt == null || plt.Count == 0){PageIndex = 1;}else{PageIndex = plt.Count + 1;}int InterCt = Ct;if (PageIndex >= Agents.Count + 1){InterCt = lt_data_site.Count - (PageIndex - 1) * Ct;}using (driver = new OpenQA.Selenium.Chrome.ChromeDriver(options)){((OpenQA.Selenium.Chrome.ChromeDriver)driver).ExecuteChromeCommand("Page.addScriptToEvaluateOnNewDocument", new Dictionary<string, object> { { "source", @"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})" } });//告诉Selenium在尝试查找元素或与页面进行交互时最多等待5秒driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);//driver.Manage().Window.Minimize();foreach (TrackingEntity a in lt_data_site.Skip((PageIndex - 1) * Ct).Take(InterCt)){try{KeyValuePair<string, Func<TrackingEntity, IWebDriver, Tracking_Chrome.TrackingRet>> par = dic.Filter(b => b.Key.Split(':')[0] == a.ShippingServer.Trim()).ToList()[0];TrackingRet ret = par.Value(a, driver);Rets.Add(ret);CurQty++;Total--;this.Invoke(new Action(() =>{toolStripStatusLabel3.Text = string.Format("剩余:{0}", Total);}));}catch (Exception ex){}}if (CurQty < lt_data_site.Count){IsChangeAgent = true;CloseChromeDriver(driver);goto change;}}}//保存数据ConsoleLog("保存数据中...");SaveData(Rets);ConsoleLog("数据保存完毕.");log(DateTime.Now.ToString() + "  数据同步完毕!");}catch(Exception ex){CloseChromeDriver(driver);log(ex.Message);ConsoleLog(ex.Message);}finally {//关闭程序Thread.Sleep(3000);Application.Exit();}}

辅助代码:

        /// <summary>/// ChromeDriver检查更新/// </summary>public void UpdateChromeDriver(){//Task t = Task.Run(() => {try{ConsoleLog("chromedriver更新检测中...");//获取当前浏览器版本string CurrentChromeVer = "";RegistryKey current = Registry.CurrentUser;RegistryKey chromeRegistry = current.OpenSubKey(@"Software\Google\Chrome\BLBeacon", false);if (chromeRegistry != null){string version = chromeRegistry.GetValue("version").DbToString();if (!string.IsNullOrWhiteSpace(version)){CurrentChromeVer = version;}}if (string.IsNullOrEmpty(CurrentChromeVer)){throw new Exception("当前浏览器版本获取失败.");}//获取ChromeDriver版本string CurrentChromeDriverVer = "";string driverPath = AppDomain.CurrentDomain.BaseDirectory + "chromedriver.exe";if (File.Exists(driverPath)){Process pro = new Process();pro.StartInfo.FileName = "cmd.exe";pro.StartInfo.UseShellExecute = false;pro.StartInfo.RedirectStandardInput = true;pro.StartInfo.RedirectStandardOutput = true;pro.StartInfo.RedirectStandardError = true;pro.StartInfo.CreateNoWindow = true;pro.Start();pro.StandardInput.WriteLine(driverPath + " --version");pro.StandardInput.WriteLine("exit");string line = null;string chromeStr = string.Empty;while ((line = pro.StandardOutput.ReadLine()) != null){line = line.Trim();if (line.StartsWith("ChromeDriver")){chromeStr = line;break;}}string outputStr = chromeStr;//输出的格式 ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads / 3904@{#800})if (!string.IsNullOrWhiteSpace(outputStr)){outputStr = outputStr.Replace("ChromeDriver", "").Trim();//78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads / 3904@{#800})int endIndex = outputStr.IndexOf(" ");if (endIndex != -1){outputStr = outputStr.Substring(0, endIndex);//78.0.3904.70CurrentChromeDriverVer = outputStr;}}}if (string.IsNullOrEmpty(CurrentChromeDriverVer)){throw new Exception("ChromeDriver版本获取失败.");}int index1 = CurrentChromeVer.LastIndexOf(".");int index2 = CurrentChromeDriverVer.LastIndexOf(".");string firstVersion1 = CurrentChromeVer.Substring(0, index1);string firstVersion2 = CurrentChromeDriverVer.Substring(0, index2);Version version1 = new Version(firstVersion1);Version version2 = new Version(firstVersion2);if (version2 < version1){string root = AppDomain.CurrentDomain.BaseDirectory + "temp";if (!System.IO.Directory.Exists(root)){System.IO.Directory.CreateDirectory(root);}//获取镜像页面版本号string newVersion = "";string url = "http://npm.taobao.org/mirrors/chromedriver/";using (System.Net.WebClient client = new System.Net.WebClient()){string html = client.DownloadString(url);HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();doc.LoadHtml(html);//查找以当前浏览器版本号的driverstring path = $"//div[@class=\"container\"]//pre//a[starts-with(text(),\"{ firstVersion1}\")]";HtmlAgilityPack.HtmlNode nodes = doc.DocumentNode.SelectNodes(path)?.LastOrDefault();if (nodes != null){newVersion = nodes.InnerText.Trim('/').Trim();}}if (string.IsNullOrEmpty(newVersion)){throw new Exception("镜像页面版本号获取失败.");}//从服务器下载chromedriverurl = $"http://npm.taobao.org/mirrors/chromedriver/{newVersion}/chromedriver_win32.zip";System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;request.Method = "GET";request.ContentType = "application/octet-stream";using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse){string fullName = root + "\\chromedriver_win32.zip";if (System.IO.File.Exists(fullName)){System.IO.File.Delete(fullName);}byte[] bytes = GetMemoryStream(response.GetResponseStream()).ToArray();using (System.IO.FileStream fs = new System.IO.FileStream(fullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)){fs.Write(bytes, 0, bytes.Length);}}//解压文件root = AppDomain.CurrentDomain.BaseDirectory + "temp\\chromedriver_win32.zip";if (File.Exists(root)){UnZip(root, AppDomain.CurrentDomain.BaseDirectory);}else{throw new Exception("文件下载失败.");}ConsoleLog("升级成功.");}else{ConsoleLog("无需升级.");}}catch (Exception ex){ConsoleLog("更新出错:" + ex.Message);log("更新出错:" + ex.Message);}//});//await t;}/// <summary>/// 解压文件/// </summary>/// <param name="zipFile"></param>/// <param name="destFolder"></param>private void UnZip(string zipFile, string destFolder){if (!destFolder.EndsWith("/"))destFolder += "/";if (!Directory.Exists(destFolder))Directory.CreateDirectory(destFolder);using (var s = new ZipInputStream(File.OpenRead(zipFile))){ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(theEntry.Name);string fileName = Path.GetFileName(theEntry.Name);if (!string.IsNullOrEmpty(directoryName)){Directory.CreateDirectory(destFolder + directoryName);}if (fileName != String.Empty){using (FileStream streamWriter = File.Create(destFolder + theEntry.Name)){int size;byte[] data = new byte[2048];while (true){size = s.Read(data, 0, data.Length);if (size > 0){streamWriter.Write(data, 0, size);}else{break;}}}}}}}private MemoryStream GetMemoryStream(Stream streamResponse){using (MemoryStream _stream = new MemoryStream()){int Length = 256;Byte[] buffer = new Byte[Length];int bytesRead = streamResponse.Read(buffer, 0, Length);while (bytesRead > 0){_stream.Write(buffer, 0, bytesRead);bytesRead = streamResponse.Read(buffer, 0, Length);}return _stream;}}/// <summary>/// 获取单个值/// </summary>/// <param name="driver"></param>/// <param name="xpath"></param>/// <returns></returns>public string ElementGetValue(IWebDriver driver, string xpath){try{int Index = xpath.LastIndexOf("/");string LastStr = xpath.Substring(Index);if (LastStr.ToLower().Contains("text()") || LastStr.IndexOf("@") == 1){xpath = xpath.Replace(LastStr, "");}string Attr = "";if (LastStr.IndexOf("@") == 1){Attr = LastStr.Substring(2);}//等待new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until<bool>((a) => {try{var cts = a.FindElements(By.XPath(xpath));return cts.Count > 0 && cts[0].Text.Trim() != "" && cts[0].Text.Trim() != "查询中";}catch{return false;}});var ele = driver.FindElements(By.XPath(xpath))[0];if (LastStr.ToLower().Contains("text()")){return ele.Text.Trim();}else if (Attr != ""){return ele.GetAttribute(Attr);}else{return ele.GetProperty("innerHTML");}}catch (Exception ex){throw ex;}}public void CloseChromeDriver(IWebDriver driver){try{driver.Quit();driver.Dispose();}catch { }CloseWindow();}/// <summary>/// 获取窗口句柄/// </summary>/// <returns></returns>public IntPtr GetWindowHandle(){string name = Path.GetDirectoryName(Application.ExecutablePath)+"\\chromedriver.exe";IntPtr hwd = FindWindow(null, name);//log(hwd.ToString());//log(name);return hwd;}/// <summary>/// 关闭chromedriver窗口/// </summary>public void CloseWindow(){try{IntPtr hwd = GetWindowHandle();SendMessage(hwd, 0x10, 0, 0);}catch { }}/// <summary>/// 切换代码/// </summary>/// <param name="site"></param>public void ChangeSite(string site="") {Agent agent = WebAgentList.Find(a => a.Site == site).AgentList.Filter(b => b.Available == 1).First();agent.Available = 0;GeneratePlug(agent.Ip, agent.Port, agent.UserName, agent.PassWord);ConsoleLog(string.Format("已切换代理=>{0}:{1}", agent.Ip, agent.Port));this.Invoke(new Action(() => {this.Text = FormTitle + string.Format("         当前使用代理=>{0}:{1}", agent.Ip, agent.Port);}));}/// <summary>/// 生成插件/// </summary>/// <param name="ip"></param>/// <param name="port"></param>/// <param name="userName"></param>/// <param name="passWord"></param>public void GeneratePlug(string ip="",string port="",string userName="",string passWord="") {try{string FileName = Path.GetDirectoryName(Application.ExecutablePath) + "\\proxy.zip";string BackGroundJs = @"var config = {mode: 'fixed_servers',rules: {singleProxy: {scheme: 'http',host: 'mimvp_proxy_host',port: parseInt(mimvp_proxy_port)},bypassList: ['{0}']}};chrome.proxy.settings.set({value: config, scope: 'regular'}, function() {});function callbackFn(details) {return {authCredentials: {username: 'mimvp_username',password: 'mimvp_password'}};}chrome.webRequest.onAuthRequired.addListener(callbackFn,{urls: ['<all_urls>']},['blocking']);".Replace("{0}", string.Join(",", Sites).Replace(",", "','"));string ManiFest = "{\"version\": \"1.0.0\",\"manifest_version\": 2,\"name\": \"Chrome Proxy\",\"permissions\": [\"proxy\",\"tabs\",\"unlimitedStorage\",\"storage\",\"<all_urls>\",\"webRequest\",\"webRequestBlocking\"],\"background\": {\"scripts\": [\"background.js\"]},\"minimum_chrome_version\":\"22.0.0\"}";BackGroundJs =BackGroundJs.Replace("mimvp_proxy_host", ip).Replace("mimvp_proxy_port", port).Replace("mimvp_username", userName).Replace("mimvp_password", passWord);List<MemoryStream> ms = new List<MemoryStream> {new MemoryStream(System.Text.Encoding.UTF8.GetBytes(BackGroundJs)),new MemoryStream(System.Text.Encoding.UTF8.GetBytes(ManiFest))};List<string> lt = new List<string> { "background.js", "manifest.json" };Compress(ms, lt, FileName);}catch (Exception ex) { }}/// <summary>/// 压缩多个文件/// </summary>/// <param name="msList">文件流</param>/// <param name="fileNameList">文件名称</param>/// <param name="GzipFileStream">压缩后的文件流</param>/// <param name="CompressionLevel">压缩等级</param>/// <param name="SleepTimer">等待的时长(毫秒)</param>public void Compress(List<MemoryStream> msList, List<string> fileNameList,string fileName){using (FileStream GzipFileStream = new FileStream(fileName,FileMode.Create,FileAccess.Write)) {using (ZipOutputStream zipStream = new ZipOutputStream(GzipFileStream)) {zipStream.SetLevel(6);int index = 0;foreach (MemoryStream ms in msList){ms.Position = 0;byte[] bytes = new byte[ms.Length];ms.Read(bytes, 0, bytes.Length);ZipEntry entry = new ZipEntry(Path.GetFileName(fileNameList[index]));entry.DateTime = DateTime.Now;zipStream.PutNextEntry(entry);zipStream.Write(bytes, 0, bytes.Length);System.Threading.Thread.Sleep(10);index++;}}}}/// <summary>/// 日志/// </summary>/// <param name="info"></param>public void log(string info){string FilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";StreamWriter sw;if (!File.Exists(FilePath)){sw = new StreamWriter(FilePath, false);}else{sw = new StreamWriter(FilePath, true);}sw.Write(info + "\r\n"); sw.Flush();sw.Close();}public void ConsoleInfo(TrackingRet tr){if (string.IsNullOrEmpty(tr.error)){ConsoleLog(string.Format("【{0}】追踪号{1}状态:{2}获取成功!", tr.ShippingServer, tr.TrackingNo, tr.TrackStatus));}else{ConsoleLog(string.Format("【{0}】追踪号{1} 信息:{2}", tr.ShippingServer, tr.TrackingNo, tr.error));}}public void ConsoleLog(string info){this.Invoke(new Action(() => {if (listBox1.Items.Count >= 1000){listBox1.Items.Clear();}listBox1.Items.Insert(0, DateTime.Now.ToString() + " " + info);}));}public void SaveData(List<TrackingRet> lt){lt.FindAll(a => string.IsNullOrEmpty(a.error) && !string.IsNullOrEmpty(a.TrackStatus)).Each(a => {try{a.TrackStatus = a.TrackStatus.Replace("'", "''").Length > 190 ? a.TrackStatus.Replace("'", "''").Substring(0, 190) : a.TrackStatus.Replace("'", "''");if (Convert.ToInt32(DbHelperSQL_SR.GetSingle(string.Format("select (1) from tUploadCustom t where t.TrackingNumber='{0}'", a.TrackingNo))) > 0){//updateDbHelperSQL_SR.ExecuteSql(string.Format("update t set t.Status='{0}',t.Lastupdate=getdate() from tUploadCustom t where t.TrackingNumber='{1}'", a.TrackStatus, a.TrackingNo));DbHelperSQL_SR.ExecuteSql(string.Format("update t set t.TrackingStatus='{0}',t.TrackingDate='{1}' from tEBay_Trade t where t.TrackingNumber='{2}'", a.TrackStatus, a.TrackingDate, a.TrackingNo));}else{string Sql = string.Format(@"INSERT INTO tUploadCustom (SerialNumber, TrackingNumber, TType, TDate, SHIP_USD, IsUpload, Remark, Uid, Status, ShopId, ShippingMethod)SELECTt.Oid,t.TrackingNumber,t.ShippingServer,getdate(),t.Fee / r2.rate fee,0,2,0,'{0}',t.ShopID,t.ShippingMethodFROM tEBay_Trade tOUTER APPLY dbo.fn_GettExchangeRateDateT(getdate(), t.ShippingCCY) R2WHERE t.TrackingNumber = '{1}'", a.TrackStatus.Replace("'", "''"), a.TrackingNo);DbHelperSQL_SR.ExecuteSql(Sql);}}catch (Exception ex){log(ex.Message + ex.StackTrace + ex.Source);}});}/// <summary>/// 正则匹配/// </summary>/// <param name="input"></param>/// <param name="reg"></param>/// <returns></returns>public string MatchStrSingle(string input, string reg){List<string> lt = new List<string>();foreach (Match item in Regex.Matches(input, reg)){lt.Add(item.Value);}if (lt.Count == 0){return "";}return lt[0];}

四、隐藏黑色的命令行窗口

     [DllImport("user32.dll", EntryPoint = "FindWindow")]private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", EntryPoint = "SendMessage")]public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);public const int SW_HIDE = 0;public const int SW_SHOW = 5;[DllImport("user32.dll", EntryPoint = "ShowWindow")]public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);public void HideWnd(){IntPtr hwd = GetWindowHandle();ShowWindow(hwd, SW_HIDE);}/// <summary>/// 获取窗口句柄/// </summary>/// <returns></returns>public IntPtr GetWindowHandle(){string name = Path.GetDirectoryName(Application.ExecutablePath) + "\\chromedriver.exe";IntPtr hwd = FindWindow(null, name);//log(hwd.ToString());//log(name);return hwd;}/// <summary>/// 关闭chromedriver窗口/// </summary>public void CloseWindow(){try{IntPtr hwd = GetWindowHandle();SendMessage(hwd, 0x10, 0, 0);}catch { }}

没错,就是使用windows API隐藏,桌面和任务栏都不会出现黑窗口。使用的时候,注意在程序最后关闭的时候即使调用CloseWindow进行窗口关闭。

Selenium.Chrome相关配置及用法相关推荐

  1. Python selenium chrome 环境配置

    Python selenium chrome 环境配置一.参考文章:1. 记录一下python easy_install和pip安装地址和方法http://heipark.iteye.com/blog ...

  2. chrome java mac下载_Mac上Java+selenium+Chrome环境配置

    前言 本人之前在window下使用Java+selenium+Firefox进行UI测试,由于window本子集显烧了更换了MacBook Pro,所以开始研究如何在Mac环境下进行UI自动化测试.但 ...

  3. springmvc相关配置和用法

    目录如下: 一.spring mvc 核心类与接口 二.spring mvc 核心流程图 三.spring mvc DispatcherServlet说明 四.spring mvc 父子上下文的说明 ...

  4. python3 + selenium + (chrome and firefox)使用

    目录 瞎扯一句 简介 最后放模板 瞎扯一句 最近在做一个关于 selenium 相关的项目,在选择浏览器方面,一般有3种方案: chrome phantomJs firefox(推荐) 网上有很多教程 ...

  5. Python + Selenium + Chrome Driver 自动化点击+评论+刷弹幕(仅供学习)

    Python + Selenium + Chrome Driver 自动化点击 评论 刷弹幕 首先说明,这篇博文仅供学习!仅供学习!仅供学习! 不要拿去做其他事,封号概不负责!!! 突发奇想 首先先说 ...

  6. wps python 自动化_Python3+Selenium+Chrome实现自动填写WPS表单

    引言 本文通过python3.第三方python库Selenium和谷歌浏览器Chrome,完成WPS表单的自动填写. 开发环境配置 python3的安装:略,网上都有教程. Selenium的安装: ...

  7. Ubuntu 无界面使用selenium chrome + headless

    Ubuntu 无界面使用selenium chrome + headless 1. 安装 selenium : sudo pip install selenium 2. 安装 chromdriver: ...

  8. 11.28 限定某个目录禁止解析php 11.29 限制user_agent 11.30/11.31 php相关配置

    - 11.28 限定某个目录禁止解析php - 11.29 限制user_agent - 11.30/11.31 php相关配置 - 扩展 - apache开启压缩 http://ask.apelea ...

  9. Nacos配置中心用法详细介绍

    上篇文章介绍了 Nacos 作为注册中心的用法,除此之外,Nacos 还能作为配置中心使用,那这篇文章就介绍下 Nacos 作为配置中心的基本用法,首先我们先了解下为什么需要使用配置中心. 一.为什么 ...

最新文章

  1. 通过修改EIP寄存器实现强行跳转并且注入DLL到目标进程里
  2. 机器学习降维方法总结
  3. 【android-tips】如何在view中取得activity对象
  4. Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
  5. php登录信息首页显示,首页登录后怎么在首页显示用户名以及隐藏登录框?
  6. java创建目录时带权限_java – 无法在外部存储中创建目录,尽管权限显然设置正确...
  7. 容器编排技术 -- 使用Vagrant本地运行Kubernetes
  8. 起底!年入2543亿,比3M更狠的口罩巨头,凭什么是它?
  9. ZooKeeper使用get命令无法获取节点版本信息
  10. Flutter进阶—质感设计之卡片
  11. 1007. 素数对猜想 (20)-PAT乙级真题
  12. java工程师写html_java工程师_基础_阶段一_HTML笔记篇
  13. linux全自动备份网站到百度云盘,Linux定时备份数据到百度云盘(示例代码)
  14. con和com开头单词
  15. Mac上最强大好用的的右键工具「iRightMouse 超级右键」(上)
  16. Ubuntu 16.04中cartographer安装过程中的troubleshooting,以及demo的实现
  17. 数显之家快讯:【SHIO世硕心语】一个人成功之前,需要失去五样东西!
  18. 2018 ICPC 北京区域赛 I - Palindromes(规律+大数+模拟)
  19. win10 桌面(Windows 资源管理器)卡死的根本解决办法
  20. 网格交易法策略优化-解决单边下跌问题,降低破网率

热门文章

  1. windows下的300个免费软件
  2. 第一个简单的JSP网页
  3. JAVA程序设计:分数加减运算(LeetCode:592)
  4. 英伟达 jetson xavier agx 开发(2)修改内核设备树
  5. 刘慈欣回应《三体》获奖nbsp;对…
  6. linux 中copy软链接
  7. 基于微信小程序的预约订座系统 开题报告
  8. 基于微信运动场地预约小程序 毕业设计毕业论文 开题报告和效果图(基于微信小程序毕业设计题目选题课题)
  9. 计算机视觉方向博后,直播回顾 | 深圳大学龚元浩:比几何流快一万倍的曲率滤波算法(附博后招聘)...
  10. 这一年,我所感悟到的