ocrClickOnText
基于提供的文本点击元素。该命令将搜索提供的文本,并尝试使用Fuse.js的模糊逻辑找到匹配项。这意味着即使你提供了带有拼写错误的选择器,或者找到的文本可能不是100%匹配,它仍然会尝试返回一个元素。查看下面的日志。
Usage
await browser.ocrClickOnText({ text: "Start3d" });
Output
Logs
# Still finding a match even though we searched for "Start3d" and the found text was "Started"
[0-0] 2024-05-25T05:05:20.096Z INFO webdriver: COMMAND ocrClickOnText(<object>)
......................
[0-0] 2024-05-25T05:05:21.022Z INFO @wdio/ocr-service:ocrGetElementPositionByText: Multiple matches were found based on the word "Start3d". The match "Started" with score "85.71%" will be used.
Image
你将在你的(默认)imagesFolder中找到一张带有目标标记的图片,显示模块点击的位置。

Options
text
- Type:
string - Mandatory: yes
你要搜索并点击的文本。
Example
await browser.ocrClickOnText({ text: "WebdriverIO" });
clickDuration
- Type:
number - Mandatory: no
- Default:
500milliseconds
这是点击的持续时间。如果需要,你也可以通过增加时间来创建"长按点击"。
Example
await browser.ocrClickOnText({
text: "WebdriverIO",
clickDuration: 3000, // This is 3 seconds
});
contrast
- Type:
number - Mandatory: no
- Default:
0.25
对比度越高,图像越暗,反之亦然。这可以帮助在图像中找到文本。它接受介于-1和1之间的值。
Example
await browser.ocrClickOnText({
text: "WebdriverIO",
contrast: 0.5,
});
haystack
- Type:
number - Mandatory:
WebdriverIO.Element | ChainablePromiseElement | Rectangle
这是屏幕上OCR需要寻找文本的搜索区域。可以是一个元素或包含x、y、width和height的矩形。
Example
await browser.ocrClickOnText({
text: "WebdriverIO",
haystack: $("elementSelector"),
});
// OR
await browser.ocrClickOnText({
text: "WebdriverIO",
haystack: await $("elementSelector"),
});
// OR
await browser.ocrClickOnText({
text: "WebdriverIO",
haystack: {
x: 10,
y: 50,
width: 300,
height: 75,
},
});
language
- Type:
string - Mandatory: No
- Default:
eng
Tesseract将识别的语言。更多信息可以在这里找到,支持的语言可以在这里找到。