/*Resolve o captcha e preenche campo com a string obtida*/ private static string GetScriptFillCaptcha(string imageId, string inputId) { RService.Log("(GetScriptFillCaptcha) " + Name + ": Resolvendo captcha... " + " at {0}", Path.GetTempPath() + Name + ".txt"); try { string tempImg = Path.GetTempPath() + DateTime.Now.ToString("yyyyMMddfff") + ".jpg"; string tempImgCrop = Path.GetTempPath() + DateTime.Now.ToString("ddMMyyyyfff") + ".jpg"; web.GetScreenshot().SaveAsFile(tempImg, ScreenshotImageFormat.Jpeg); Bitmap image = (Bitmap)Image.FromFile(tempImg); GetCaptchaImg(web.FindElement(By.Id(imageId)), image, tempImgCrop); string script = string.Format("document.getElementById('{0}').value = '{1}'", inputId, WebHandle.ResolveCaptcha(tempImgCrop)); if (File.Exists(tempImg)) { File.Delete(tempImg); } if (File.Exists(tempImgCrop)) { File.Delete(tempImgCrop); } NumCaptcha++; return(script); } catch (Exception e) { RService.Log("Exception (GetScriptFillCaptcha) " + Name + ": " + e.Message + " / " + e.StackTrace + " / " + e.InnerException + " at {0}", Path.GetTempPath() + Name + ".txt"); return(null); } }
private static string GetScriptFillCaptcha(ChromeDriver web, string input) { RService.Log("(GetScriptFillCaptcha) " + Name + ": Resolvendo captcha... at {0}", Path.GetTempPath() + Name + ".txt"); string script = string.Empty; Bitmap image; try { string tempImg = Path.GetTempPath() + "tempImg.png"; string tempImgCrop = Path.GetTempPath() + "tempImgCrop.png"; web.GetScreenshot().SaveAsFile(tempImg, ScreenshotImageFormat.Png); using (Stream bmpStream = File.Open(tempImg, FileMode.Open)) { Image img = Image.FromStream(bmpStream); image = new Bitmap(img); } GetCaptchaImg(web.FindElement(By.XPath("//*[@id=\"form1\"]/div[1]/img")), image, tempImgCrop); script = WebHandle.ResolveCaptcha(tempImgCrop); if (File.Exists(tempImg)) { File.Delete(tempImg); } if (File.Exists(tempImgCrop)) { File.Delete(tempImgCrop); } NumCaptcha++; } catch (Exception ex) { RService.Log("Exception (GetScriptFillCaptcha) " + Name + ": " + ex.Message + " / " + ex.StackTrace + " / " + ex.InnerException + " at {0}", Path.GetTempPath() + Name + ".txt"); } return(script); }
/*Resolve o captcha e preenche campo com a string obtida*/ private static string GetScriptFillCaptchaXPath(string imageXPath, string inputId) { RService.Log("(GetScriptFillCaptcha) " + GetNameRobot() + ": Resolvendo captcha... " + " at {0}", Path.GetTempPath() + GetNameRobot() + ".txt"); string tempImg = string.Empty, tempImgCrop = string.Empty; try { tempImg = Path.GetTempPath() + Guid.NewGuid().ToString() + ".jpg"; tempImgCrop = Path.GetTempPath() + Guid.NewGuid().ToString() + ".jpg"; web.GetScreenshot().SaveAsFile(tempImg, ScreenshotImageFormat.Jpeg); Bitmap image = (Bitmap)Image.FromFile(tempImg); GetCaptchaImg(web.FindElement(By.XPath(imageXPath)), image, tempImgCrop); string script = string.Format("document.getElementById('{0}').value = '{1}'", inputId, WebHandle.ResolveCaptcha(tempImgCrop).ToLower()); NumCaptcha++; return(script); } catch (Exception e) { RService.Log("Exception (GetScriptFillCaptcha) " + GetNameRobot() + ": " + e.Message + " / " + e.StackTrace + " / " + e.InnerException + " at {0}", Path.GetTempPath() + GetNameRobot() + ".txt"); return(null); } finally { if (File.Exists(tempImg)) { File.Delete(tempImg); } if (File.Exists(tempImgCrop)) { File.Delete(tempImgCrop); } } }