private static Boolean sendingMessages(ChromeDriver driver, Random random, String connString, SettingsInfo setting) { String status = Utilities.Utilities.GetCurrentMethodName(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Int32 countSuccess = 0; Boolean success = false; display(String.Format("Sending [{0}]", setting.OpenPages), false, false); WebDriverWait wait = new WebDriverWait(driver, _TimeSpanWaiting); List <SupplierInfo> suppliers = DataOperation.GetSuppliersToPost(connString, setting.OpenPages); display(String.Format("-> ", ""), false, false); foreach (SupplierInfo supplier in suppliers) { //supplier.ContactProfileUrl = "https://easco.en.alibaba.com/contactinfo.html"; supplier.Account = setting.Account; String url = getSendingUrl(driver, wait, supplier); if (!String.IsNullOrEmpty(url)) { try { success = driver.GoToUrl(url); if (driver.PageSource.Contains("board errorA")) { supplier.Errors[status] = Utilities.WebDriverExtension.GetElementValue(driver, wait, "//div[@class='board errorA']/h3"); supplier.FatalError = true; } else { IWebElement element = null; if (driver.GetElement("//input[@name='email']", ref element)) { supplier.Errors[status] = "Need email as verification"; supplier.FatalError = true; } else { String newMessage = getMessageToSend(supplier, driver, wait); Utilities.WebDriverExtension.CheckElement(driver, wait, @"//input[@id='respond-in-oneday']", false); Utilities.WebDriverExtension.CheckElement(driver, wait, @"//input[@id='agree-share-bc']", true); //System.Threading.Thread.Sleep(random.Next(1000, 2000)); //driver.Navigate().Refresh(); IList <IWebElement> iFramList = driver.FindElementsByTagName("iframe"); driver.SwitchTo().Frame(0); if (!driver.GetElement("//body[@id='tinymce']", ref element, "tinymce")) { driver.Navigate().Refresh(); //if (!driver.GetElement("//textarea[@id='inquiry-content']", ref element)) if (!driver.GetElement("//form[@class='inquiry-body']/div/div/div/textarea", ref element)) { supplier.Errors[status] = "New Format!!"; //try //{ // element = driver.FindElementById("inquiry-content"); //} //catch (System.Exception) //{ // element = driver.FindElement(By.XPath("//textarea[@id='inquiry-content']")); //} } else { sendMessage(newMessage, supplier, driver, wait, element, random); } } else { sendMessage(newMessage, supplier, driver, wait, element, random); } } } if (!supplier.HasError) { ++countSuccess; success = true; } } catch (System.Exception ex) { supplier.Errors[status] = ex.Message; } } DataOperation.UpdateSupplierPostStatus(connString, supplier); Utilities.Utilities.Log(message: supplier.HasError ? "X" : ".", isWriteLine: false, addTime: false, isError: supplier.HasError); if (supplier.FatalError) { success = false; break; } } stopWatch.Stop(); Int64 averageTicks = countSuccess == 0 ? stopWatch.Elapsed.Ticks : stopWatch.Elapsed.Ticks / countSuccess; display($@"Done [{countSuccess}] [{averageTicks:c}]/per."); return(success); }
public static void SendingAndPolling(String connString, string[] args) { WebDriverSettingInfo webDriverSetting = new WebDriverSettingInfo(hideCommand: false, hideBrowser: false); Int32 totalAccount = 1; Boolean isAll = true; if (args.Length > 0) { totalAccount = Convert.ToInt32(args[0]); if (args.Length == 2) { isAll = args[1].ToBoolean(); } } String status = Utilities.Utilities.GetCurrentMethodName(); Boolean success = false; Random random = new Random(); display(message: $"[{status}] Sending And Polling Start ... [{totalAccount}]", isWriteLine: true, addTime: true, feedLine: true); Int32 count = 1; while (count <= totalAccount) { try { ChromeDriver driver = Utilities.WebDriverExtension.GetChromeDriver(hideBrowser: webDriverSetting.HideBrowser, hideCommand: webDriverSetting.HideCommand, isMaximized: true); if (driver != null) { SettingsInfo setting = DataOperation.GetSetting(connString); //setting.Email = "*****@*****.**"; //setting.Password = "******"; display(String.Format("[{0}/{1}] [{2}] ....", count, totalAccount, setting.Email), false, true); if (alibabaLogin(driver, connString, setting, random.Next(4000, 6000))) { ++count; success = DataOperation.UpdateSettingPostStatus(connString, setting); if (sendingMessages(driver, random, connString, setting)) { if (isAll) { success = pollingBusinessCardSent(driver, random, connString, setting); success = pollingBusinessCardReceived(driver, random, connString, setting); } } else { display($"[{status}] Failed with email verification.", true, true); } } else { display($"[{status}] Login Failed.", true, true); } } driver.Quit(); if (_Stop) { break; } } catch (System.Exception ex) { display($"[{status}] ERROR: {ex.Message}", true, true); } } }
private static Boolean pollingSupplierUrls(String connString, ChromeDriver driver, WebDriverWait wait, SupplierCategoryInfo supplierCategory) { Boolean success = true; Random random = new Random(); Int32 totalPage, totalPageLast, currentPage; Int32 count = 0; String status = Utilities.Utilities.GetCurrentMethodName(); // "pollingSupplierUrls"; try { driver.Navigate().GoToUrl(supplierCategory.Url); if (driver.PageSource.Contains("dpl-board-alert-large")) { supplierCategory.Errors[status] = driver.GetElementValue(wait, "//div[@class='dpl-board-alert-large']/strong"); } else { totalPage = getTotalPages(driver, wait); totalPageLast = 0; //String rootUrl = String.Empty; if (totalPage > 0) { //rootUrl = driver.GetElementValue(wait, "//div[@class='ui2-pagination-pages']/a[@class='next']", "href"); //rootUrl = rootUrl.Substring(0, rootUrl.Length - 1); do { currentPage = getCurrentPage(driver, wait, "//div[@class='ui2-pagination-pages']/span[@class='current']"); display(totalPage == totalPageLast? $"[{currentPage}]": $"[{currentPage}/{totalPage}]", isWriteLine: false, isPositionKept: false); totalPageLast = totalPage; //if (currentPage < 29) //{ // currentPage = 29; // driver.Navigate().GoToUrl("https://www.alibaba.com/catalogs/corporations/CID141905/29"); //} List <String> urlList = getUrlList(driver, wait); if (urlList.Count > 0) { count += urlList.Count; updateSupplierUrls(connString, supplierCategory, urlList); //display(String.Format(".", ""), false, false); } //String gotoUrl = String.Format("{0}{1}", rootUrl, currentPage + 1); String gotoUrl = driver.GetElementValue(wait, "//div[@class='ui2-pagination-pages']/a[@class='next']", "href"); if (!String.IsNullOrEmpty(gotoUrl)) { //if (currentPage < totalPage && gotoUrl.IsWebSiteAvailable()) //{ driver.Navigate().GoToUrl(gotoUrl); if (driver.PageSource.Contains("ui2-pagination-pages")) { totalPage = getTotalPages(driver, wait); if (totalPage < 0) { totalPage = currentPage; } } else { break; } //} //else break; } else { break; } } while (true); } } success = DataOperation.UpdateSupplierCategoryStatus(connString, supplierCategory); //System.Threading.Thread.Sleep(random.Next(20000, 30000)); display(supplierCategory.HasError ? "[XX]" : $"[{supplierCategory.CountAdded}/{count}] Done", false, false); } catch (System.Exception ex) { display(String.Format("\n[pollingSuppliers] ERROR: {0}", ex.Message), true, true); success = false; } return(success); }