元素拾取泛化动态记录id: 长数字结尾唯一id自动转为前缀匹配
- 淘宝等站点卡片根节点 id 为动态记录 id(如 item_id_1087512368188), 原样生成导致选择器只命中一张卡 - stable() 检测 id 以长数字(6位以上)结尾时, 若 [id^=前缀] 命中多个元素则优先采用, 使选择器覆盖所有同类卡片 - 拾取器与元素引用两处 cssPath 同步修改
This commit is contained in:
parent
309898045c
commit
cf1e560802
@ -297,8 +297,18 @@ export class BrowserSession implements BrowserLike {
|
||||
const localPath = (start, root) => {
|
||||
const stable = (item) => {
|
||||
if (item.id) {
|
||||
// 长数字结尾的唯一 id 多为动态记录 id(如淘宝 item_id_1087512368188),泛化为前缀匹配以命中所有同类卡片
|
||||
const dynamic = /^(.*?[-_])\d{6,}$/.exec(item.id);
|
||||
if (dynamic) {
|
||||
const generalized = '[id^="' + cssEscape(dynamic[1]) + '"]';
|
||||
try { if (root.querySelectorAll(generalized).length > 1) return generalized; } catch {}
|
||||
}
|
||||
const candidate = '#' + cssEscape(item.id);
|
||||
try { if (root.querySelectorAll(candidate).length === 1) return candidate; } catch {}
|
||||
if (dynamic) {
|
||||
const generalized = '[id^="' + cssEscape(dynamic[1]) + '"]';
|
||||
try { if (root.querySelectorAll(generalized).length >= 1) return generalized; } catch {}
|
||||
}
|
||||
}
|
||||
const testId = item.getAttribute('data-testid');
|
||||
if (testId) {
|
||||
@ -1276,8 +1286,18 @@ const PICKER_BOOTSTRAP_SOURCE = `(() => {
|
||||
const localPath = (start, root) => {
|
||||
const stable = (item) => {
|
||||
if (item.id) {
|
||||
// 长数字结尾的唯一 id 多为动态记录 id(如淘宝 item_id_1087512368188),泛化为前缀匹配以命中所有同类卡片
|
||||
const dynamic = /^(.*?[-_])\d{6,}$/.exec(item.id);
|
||||
if (dynamic) {
|
||||
const generalized = '[id^="' + cssEscape(dynamic[1]) + '"]';
|
||||
try { if (root.querySelectorAll(generalized).length > 1) return generalized; } catch {}
|
||||
}
|
||||
const candidate = '#' + cssEscape(item.id);
|
||||
try { if (root.querySelectorAll(candidate).length === 1) return candidate; } catch {}
|
||||
if (dynamic) {
|
||||
const generalized = '[id^="' + cssEscape(dynamic[1]) + '"]';
|
||||
try { if (root.querySelectorAll(generalized).length >= 1) return generalized; } catch {}
|
||||
}
|
||||
}
|
||||
const testId = item.getAttribute('data-testid');
|
||||
if (testId) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user