Skip to content

Commit

Permalink
Merge pull request EmergenceAI#97 from EmergenceAI/improve_enter_text…
Browse files Browse the repository at this point in the history
…_for_mac

clear field before entry. Helps mac
  • Loading branch information
teaxio authored Aug 23, 2024
2 parents fd4c642 + cc0a2a8 commit b2886b2
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions ae/core/skills/enter_text_using_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,25 @@ async def custom_fill_element(page: Page, selector: str, text_to_enter: str):
relies on these events being fired, additional steps may be needed to simulate them.
"""
selector = f"{selector}" # Ensures the selector is treated as a string
await page.evaluate("""(inputParams) => {
const selector = inputParams.selector;
let text_to_enter = inputParams.text_to_enter;
text_to_enter = text_to_enter.trim();
document.querySelector(selector).value = text_to_enter;
}""", {"selector": selector, "text_to_enter": text_to_enter})
try:
result = await page.evaluate(
"""(inputParams) => {
const selector = inputParams.selector;
let text_to_enter = inputParams.text_to_enter;
text_to_enter = text_to_enter.trim();
const element = document.querySelector(selector);
if (!element) {
throw new Error(`Element not found: ${selector}`);
}
element.value = text_to_enter;
return `Value set for ${selector}`;
}""",
{"selector": selector, "text_to_enter": text_to_enter},
)
logger.debug(f"custom_fill_element result: {result}")
except Exception as e:
logger.error(f"Error in custom_fill_element, Selector: {selector}, Text: {text_to_enter}. Error: {str(e)}")
raise

async def entertext(entry: Annotated[EnterTextEntry, "An object containing 'query_selector' (DOM selector query using mmid attribute e.g. [mmid='114']) and 'text' (text to enter on the element)."]) -> Annotated[str, "Explanation of the outcome of this operation."]:
"""
Expand Down Expand Up @@ -121,6 +134,20 @@ def detect_dom_changes(changes:str): # type: ignore

subscribe(detect_dom_changes)

await page.evaluate(
"""
(selector) => {
const element = document.querySelector(selector);
if (element) {
element.value = '';
} else {
console.error('Element not found:', selector);
}
}
""",
query_selector,
)

result = await do_entertext(page, query_selector, text_to_enter)
await asyncio.sleep(0.1) # sleep for 100ms to allow the mutation observer to detect changes
unsubscribe(detect_dom_changes)
Expand Down

0 comments on commit b2886b2

Please sign in to comment.