ocrGetElementPositionByText
取得螢幕上文字的位置。 此命令將搜尋提供的文字,並嘗試根據 Fuse.js 的模糊邏輯尋找符合項目。 這表示,如果您提供的選擇器有錯字,或找到的文字可能不是 100% 符合,它仍會嘗試回傳給您一個元素。 請參閱下方的記錄。
用法
const result = await browser.ocrGetElementPositionByText("Username");
console.log("result = ", JSON.stringify(result, null, 2));
輸出
結果
result = {
"dprPosition": {
"left": 373,
"top": 606,
"right": 439,
"bottom": 620
},
"filePath": ".tmp/ocr/desktop-1716658199410.png",
"matchedString": "Started",
"originalPosition": {
"left": 373,
"top": 606,
"right": 439,
"bottom": 620
},
"score": 85.71,
"searchValue": "Start3d"
}
記錄
# Still finding a match even though we searched for "Start3d" and the found text was "Started"
[0-0] 2024-05-25T17:29:59.179Z INFO webdriver: COMMAND ocrGetElementPositionByText(<object>)
......................
[0-0] 2024-05-25T17:29:59.993Z INFO @wdio/ocr-service:ocrGetElementPositionByText: Multiple matches were found based on the word "Start3d". The match "Started" with score "85.71%" will be used.
選項
text
- 類型:
string
- 必要: 是
您想要搜尋以點擊的文字。
範例
await browser.ocrGetElementPositionByText({ text: "WebdriverIO" });
contrast
- 類型:
number
- 必要: 否
- 預設值:
0.25
對比度越高,影像越暗,反之亦然。 這有助於在影像中找到文字。 它接受介於 -1
和 1
之間的值。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
contrast: 0.5,
});
haystack
- 類型:
number
- 必要:
WebdriverIO.Element | ChainablePromiseElement | Rectangle
這是螢幕上 OCR 需要搜尋文字的區域。 這可以是一個元素或包含 x
、y
、width
和 height
的矩形。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
haystack: $("elementSelector"),
});
// OR
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
haystack: await $("elementSelector"),
});
// OR
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
haystack: {
x: 10,
y: 50,
width: 300,
height: 75,
},
});
language
- 類型:
string
- 必要: 否
- 預設值:
eng
Tesseract 將識別的語言。 更多資訊可以在這裡找到,而支援的語言可以在這裡找到。
範例
import { SUPPORTED_OCR_LANGUAGES } from "@wdio/ocr-service";
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
// Use Dutch as a language
language: SUPPORTED_OCR_LANGUAGES.DUTCH,
});
fuzzyFindOptions
您可以使用下列選項更改模糊邏輯來尋找文字。 這可能會有助於找到更好的符合項目
fuzzyFindOptions.distance
- 類型:
number
- 必要: 否
- 預設值 100
決定符合項目與模糊位置(由位置指定)的接近程度。 距離模糊位置距離字元的完全字母符合會被評為完全不符合。 距離為 0 時,要求符合項目位於指定的確切位置。 距離為 1000 時,要求完美的符合項目必須位於使用 0.8 閾值的 800 個字元內才能找到。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
distance: 20,
},
});
fuzzyFindOptions.location
- 類型:
number
- 必要: 否
- 預設值 0
決定模式預期會在文字中大約找到的位置。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
location: 20,
},
});
fuzzyFindOptions.threshold
- 類型:
number
- 必要: 否
- 預設值 0.6
匹配演算法會在什麼時間點放棄。 閾值為 0 時,需要完美符合(字母和位置),閾值為 1.0 時會符合任何項目。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
threshold: 0.8,
},
});
fuzzyFindOptions.isCaseSensitive
- 類型:
boolean
- 必要: 否
- 預設值: false
搜尋是否應區分大小寫。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
isCaseSensitive: true,
},
});
fuzzyFindOptions.minMatchCharLength
- 類型:
number
- 必要: 否
- 預設值 2
只會傳回長度超過此值的符合項目。(例如,如果您想要忽略結果中的單字元符合項目,請將其設定為 2)
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
minMatchCharLength: 5,
},
});
fuzzyFindOptions.findAllMatches
- 類型:
number
- 必要: 否
- 預設值: false
當 true
時,即使在字串中已找到完美符合項目,匹配函式也會繼續執行到搜尋模式的結尾。
範例
await browser.ocrGetElementPositionByText({
text: "WebdriverIO",
fuzzyFindOptions: {
findAllMatches: 100,
},
});