0
我想用Selenium Webdriver和Aspose Ocr來解決基本的驗證碼圖像。但有時候Ocr無法正確讀取圖像文本,所以我需要重複該過程或創建一個循環,直到驗證碼解決。Selenium循環通過Ocr過程,直到Captcha圖像文本解決
任何想法如何?我已經嘗試了try-catch循環,但沒有得到預期的結果。
如果驗證碼輸入正確,它將更改爲css值以顯示none,否則爲顯示塊並顯示錯誤消息以表明它是錯誤的。
在此先感謝您的答案。我沒有信譽的投你的答案,對不起,太:))
我的代碼如下:
ITakesScreenshot ssdriver = driver as ITakesScreenshot;
byte[] screenshot = ssdriver.GetScreenshot().AsByteArray;
MemoryStream ms = new MemoryStream(screenshot);
IWebElement my_image = driver.FindElement(By.XPath("//*[@id=\"Captcha\"]/img"));
Point point = my_image.Location;
int width = my_image.Size.Width;
int height = my_image.Size.Height;
Rectangle section = new Rectangle(point, new Size(width, height));
Bitmap originalScreenshot = (Bitmap)Bitmap.FromStream(ms);
Bitmap final_image = CropImage(originalScreenshot, section);
MemoryStream ms2 = new MemoryStream();
final_image.Save(ms2, ImageFormat.Png);
byte[] captchaimage = ms2.ToArray();
Image image = Image.FromStream(ms2);
OcrEngine ocrEngine = new OcrEngine();
ocrEngine.Image = ImageStream.FromStream(ms2, ImageStreamFormat.Png);
string SolvedCaptcha = "";
if (ocrEngine.Process())
{
string OcrCaptcha = ocrEngine.Text.ToString().Trim();
SolvedCaptcha = Regex.Replace(OcrCaptcha, "[^a-zA-Z0-9]", "").Trim();
}
IWebElement captcha = driver.FindElement(By.XPath("//*[@id=\"ContentPlaceHolder1_txtCaptcha\"]"));
captcha.SendKeys(SolvedCaptcha);
captcha.SendKeys(Keys.Enter);