while
(true)
{
Console.WriteLine(
"Sign: 当前进程是:"
+
Process.GetCurrentProcess()
+
", 线程号是:"
+
Thread.CurrentThread.ManagedThreadId);
Monitor.Enter(this);
distance
=
Match(AppDomain.CurrentDomain.BaseDirectory
+
"\\template.png"
, AppDomain.CurrentDomain.BaseDirectory
+
"\\target.png"
);
Console.WriteLine(
"匹配检测出的距离是:"
+
distance);
Monitor.Exit(this);
/
/
这是滑块
var slide
=
wd.FindElement(By.ClassName(
"ui-slider-btn"
));
Actions action
=
new (wd);
/
/
点击并按住滑块元素
action.ClickAndHold(slide);
/
/
action.MoveByOffset(distance,
0
);
MoveSlideByOffSet(action, distance);
string alert;
try
{
/
/
解决了为什么进入两次的bug。以前在
else
里面进行release和perform导致在
try
里面没有对滑块
/
/
进行任何的操作。
action.Release().Perform();
Thread.Sleep(
2000
);
alert
=
wd.FindElement(By.ClassName(
"ui-slider-text"
)).Text;
Console.WriteLine(alert);
}
catch (Exception e)
{
Console.WriteLine(
"发生异常:"
+
e.ToString());
alert
=
"";
}
if
(alert.Contains(
"验证成功"
))
{
Console.WriteLine(
"滑块验证成功, 移动的距离是:"
+
distance);
break
;
}
else
{
Console.WriteLine(
"滑块验证失败, 移动的距离是:"
+
distance);
wd.SwitchTo().DefaultContent();
}
Thread.Sleep(
2000
);
}
public static
int
Match(string template, string target)
{
/
/
模板图片
Mat temp
=
new Mat(template, ImreadModes.AnyColor);
/
/
被匹配图
Mat wafer
=
new Mat(target, ImreadModes.AnyColor);
/
/
Canny边缘检测
Mat temp_canny_Image
=
new Mat();
Cv2.Canny(temp, temp_canny_Image,
100
,
200
);
Mat wafer_canny_Image
=
new Mat();
Cv2.Canny(wafer, wafer_canny_Image,
100
,
200
);
/
/
匹配结果
Mat result
=
new Mat();
/
/
模板匹配
Cv2.MatchTemplate(wafer_canny_Image, temp_canny_Image, result, TemplateMatchModes.CCoeffNormed);
/
/
最好匹配为
1
,值越小匹配越差
/
/
数组位置下x,y
Point minLoc
=
new Point(
0
,
0
);
Point maxLoc
=
new Point(
0
,
0
);
Point matchLoc
=
new Point(
0
,
0
);
Cv2.MinMaxLoc(result, out minLoc, out maxLoc);
matchLoc
=
maxLoc;
return
matchLoc.X;
}