我正在尝试使用Selenium Webdriver和Aspose Ocr解决基本的验证码图像。但有时Ocr无法正确读取图像文本,因此我需要重复该过程或创建一个循环,直到验证码被解决。
任何想法如何?我已经用while尝试了try-catch循环,但没有得到预期的结果。
如果正确输入验证码,它将更改为css值以显示无,否则它是显示块并提供错误消息以指示它是错误的。
提前感谢您的答案。我对你的答案投票没有信誉,对此也很抱歉:))
我的代码如下:
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);
答案 0 :(得分:0)
我把它包裹进去。 Clear()对我没有用,所以我需要在设置即将发布之前添加js来清除输入值。这对我有用,希望对别人有帮助。每当我尝试保存它时,我都无法使用保存验证码图像版本bc更改图像。所以我选择了截屏解决方案。
b = ...
X = ...
Y = array([]).dot(X)
if Y.size:
# You have a non-empty result