From 247ee7d3dd1015c810172dbdef60c41b14d9c35f Mon Sep 17 00:00:00 2001 From: LawyZheng Date: Thu, 5 Dec 2024 14:10:30 +0800 Subject: [PATCH] optimize element context parse (#1323) --- skyvern/webeye/scraper/domUtils.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/skyvern/webeye/scraper/domUtils.js b/skyvern/webeye/scraper/domUtils.js index 16c8798b..b0c75577 100644 --- a/skyvern/webeye/scraper/domUtils.js +++ b/skyvern/webeye/scraper/domUtils.js @@ -889,10 +889,15 @@ function checkDisabledFromStyle(element) { return false; } -function getElementContext(element) { +// element should always be the parent of stopped_element +function getElementContext(element, stopped_element) { // dfs to collect the non unique_id context let fullContext = new Array(); + if (element === stopped_element) { + return fullContext; + } + // sometimes '*' shows as an after custom style const afterCustom = getElementComputedStyle(element, "::after") .getPropertyValue("content") @@ -915,7 +920,7 @@ function getElementContext(element) { } } else if (child.nodeType === Node.ELEMENT_NODE) { if (!child.hasAttribute("unique_id") && isElementVisible(child)) { - childContext = getElementContext(child); + childContext = getElementContext(child, stopped_element); } } if (childContext.length > 0) { @@ -1320,18 +1325,18 @@ function buildElementTree(starter = document.body, frame, full_tree = false) { } const getContextByParent = (element, ctx) => { - // for most elements, we're going 10 layers up to see if we can find "label" as a parent + // for most elements, we're going 5 layers up to see if we can find "label" as a parent // if found, most likely the context under label is relevant to this element let targetParentElements = new Set(["label", "fieldset"]); - // look up for 10 levels to find the most contextual parent element + // look up for 5 levels to find the most contextual parent element let targetContextualParent = null; let currentEle = getDOMElementBySkyvenElement(element); if (!currentEle) { return ctx; } let parentEle = currentEle; - for (var i = 0; i < 10; i++) { + for (var i = 0; i < 5; i++) { parentEle = parentEle.parentElement; if (parentEle) { if ( @@ -1355,10 +1360,10 @@ function buildElementTree(starter = document.body, frame, full_tree = false) { // fieldset is usually within a form or another element that contains the whole context targetContextualParent = targetContextualParent.parentElement; if (targetContextualParent) { - context = getElementContext(targetContextualParent); + context = getElementContext(targetContextualParent, currentEle); } } else { - context = getElementContext(targetContextualParent); + context = getElementContext(targetContextualParent, currentEle); } if (context.length > 0) { ctx.push(context); @@ -1440,13 +1445,13 @@ function buildElementTree(starter = document.body, frame, full_tree = false) { ) { let grandParentElement = parentElement.parentElement; if (grandParentElement) { - let context = getElementContext(grandParentElement); + let context = getElementContext(grandParentElement, curElement); if (context.length > 0) { ctx.push(context); } } } - let context = getElementContext(parentElement); + let context = getElementContext(parentElement, curElement); if (context.length > 0) { ctx.push(context); }