From 7890b4b3cadf9f130c16f9ebe056f72f28fceadf Mon Sep 17 00:00:00 2001 From: martin legrand Date: Wed, 2 Apr 2025 15:43:51 +0200 Subject: [PATCH] feat : better form handling --- sources/agents/code_agent.py | 14 +++++++++++++- sources/browser.py | 24 ++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/sources/agents/code_agent.py b/sources/agents/code_agent.py index e0e1efe..80b1f72 100644 --- a/sources/agents/code_agent.py +++ b/sources/agents/code_agent.py @@ -1,3 +1,4 @@ +import platform, os from sources.utility import pretty_print, animate_thinking from sources.agents.agent import Agent, executorResult @@ -20,6 +21,7 @@ class CoderAgent(Agent): "go": GoInterpreter(), "file_finder": FileFinder() } + self.work_dir = self.tools["file_finder"].get_work_dir() self.role = { "en": "code", "fr": "codage", @@ -27,10 +29,20 @@ class CoderAgent(Agent): } self.type = "code_agent" + + def add_sys_info_prompt(self, prompt): + """Add system information to the prompt.""" + info = f"System Info:\n" \ + f"OS: {platform.system()} {platform.release()}\n" \ + f"Python Version: {platform.python_version()}\n" \ + f"\nYou must work in directory: {self.work_dir}" + return f"{prompt}\n\n{info}" + def process(self, prompt, speech_module) -> str: answer = "" attempt = 0 - max_attempts = 3 + max_attempts = 4 + prompt = self.add_sys_info_prompt(prompt) self.memory.push('user', prompt) clarify_trigger = "REQUEST_CLARIFICATION" diff --git a/sources/browser.py b/sources/browser.py index d144ebb..a8af6b1 100644 --- a/sources/browser.py +++ b/sources/browser.py @@ -268,7 +268,7 @@ class Browser: except Exception as e: raise e - def find_all_inputs(self, timeout=4): + def find_all_inputs(self, timeout=3): WebDriverWait(self.driver, timeout).until( EC.presence_of_element_located((By.TAG_NAME, "body")) ) @@ -291,14 +291,17 @@ class Browser: continue input_name = element["text"] or element["id"] or input_type if input_type == "checkbox" or input_type == "radio": - checked_status = "checked" if element.is_selected() else "unchecked" + try: + checked_status = "checked" if element.is_selected() else "unchecked" + except Exception as e: + continue form_strings.append(f"[{input_name}]({checked_status})") else: form_strings.append(f"[{input_name}]("")") return form_strings except Exception as e: - return [f"Error extracting form inputs."] + raise e def get_buttons_xpath(self) -> List[str]: """ @@ -312,13 +315,14 @@ class Browser: continue text = (button.text or button.get_attribute("value") or "").lower().replace(' ', '') xpath = f"(//button | //input[@type='submit'])[{i + 1}]" - if "login" in text or "sign" in text or "register": - result.append((text, xpath)) + result.append((text, xpath)) result.sort(key=lambda x: len(x[0])) return result def find_and_click_submission(self, timeout: int = 10) -> bool: - possible_submissions = ["login", "submit", "register"] + possible_submissions = ["login", "submit", "register", "calculate", "login", "submit", "register", "calculate", "save", "send", + "continue", "apply", "ok", "confirm", "next", "proceed", "accept", "agree", "yes", "no", "cancel", + "close", "done", "finish", "start", "calculate"] for submission in possible_submissions: if self.find_and_click_btn(submission, timeout): return True @@ -392,6 +396,7 @@ class Browser: element.click() self.logger.info(f"Set {name} to {value}") else: + element.clear() element.send_keys(value) self.logger.info(f"Filled {name} with {value}") return True @@ -443,13 +448,12 @@ if __name__ == "__main__": browser = Browser(driver, anticaptcha_manual_install=True) time.sleep(10) - print("AntiCaptcha Test") + print("AntiCaptcha / Form Test") browser.go_to("https://www.google.com/recaptcha/api2/demo") + #browser.go_to("https://practicetestautomation.com/practice-test-login/") time.sleep(10) - print("Form Test:") - browser.go_to("https://practicetestautomation.com/practice-test-login/") inputs = browser.get_form_inputs() - inputs = ['[username](student)', f'[password](Password123)', '[appOtp]()', '[backupOtp]()'] + inputs = ['[input1](Martin)', f'[input2](Test)', '[input3](test@gmail.com)'] browser.fill_form_inputs(inputs) browser.find_and_click_submission() time.sleep(30)