My browser automation agent is stuck in an endless loop and throwing Pydantic errors, how do I actually fix this?
So I tried setting up a LangChain Playwright agent to automate posting to Reddit, and man... it is driving me insane.
It starts off fine, navigates to the links, but then it completely loses its mind trying to find input fields. It just keeps looping—retrying get_elements, going back and forth between pages, waiting for 5 seconds over and over again, and getting []every single time. And then to top it all off, it finally crashes completely with a Pydantic validation error (Field required [type=missing, input_value={'element': ...}) because it tried passing weird keys into the click tool instead of a simple CSS selector.
Has anyone actually gotten LangChain's tool-calling agent with Playwright to work reliably on modern dynamic sites like Reddit? Or am I missing something obvious about how it handles selectors and page states? Any advice from people who've dealt with this would be awesome.
code :
from mlx_lm import load, generate
import os
from dotenv import load_dotenv
from langchain.agents import create_agent, AgentState
from langchain.messages import HumanMessage, ToolMessage, AIMessage
from langchain_core.prompts import PromptTemplate
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
from langchain_community.tools.playwright.utils import create_sync_playwright_browser
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import ChatOllama
import re
from langchain_classic.agents import AgentExecutor, create_tool_calling_agent
load_dotenv()
model, tokeniser = load("mlx-community/Qwen2.5-7B-Instruct-4bit")
system_prompt = """You are a human writing assistant that rewrites a user's raw thoughts into a natural Reddit post.
Strict rules you must follow:
- Stay extremely faithful to the original message. Do not invent new problems, new details, new causes, or new solutions.
- Do not add advice, diagnoses, or explanations the user did not already express.
- Keep every important point the user made (the rewinding, the 50-minute pomodoro, the background noise, the feeling of being lost, the endless loop, the self-frustration).
- Only elaborate and expand what is already there so it flows like a real person talking.
- Make it sound completely human: messy, conversational, slightly self-deprecating, frustrated, and natural. Use contractions, incomplete thoughts, and everyday language.
- Avoid any robotic phrasing, corporate words, lists, bullet points, or overly clean structure.
- Avoid the use of emojis.
- The final post must feel like it was written by the same person who wrote the original message, just cleaned up and expanded a little for Reddit.
Output format (very important):
Title: <short natural title>
Body:
<the full reddit post>
"""
query = '''I m so dumb in studies. i keeps on rewinding the same youtube lecture like 15 times even after setting a pomodoro of 50 minutes and playing background noises. but still i can't seem to focus and ends up being lost once again just to find that i need to watch this same video again. and this loops keeps going. Why does it happen ? and is it possible to stop this ?'''
prompt = f"{system_prompt}\n\nUser's raw thoughts: {query}\n\nGenerated Reddit Post:"
raw_output = generate(
model,
tokenizer= tokeniser,
prompt=prompt,
verbose=False,
)
title_match = re.search(r"Title:\s*(.+)", raw_output)
body_match = re.search(r"Body:\s*(.*?)(?=\nTitle:|$)", raw_output, re.DOTALL)
if title_match:
title = title_match.group(1).strip()
else:
title_prompt = f"""Generate only a short, natural, human-sounding Reddit title for this post.
Do not use quotes. Do not write anything else. Just the title.
Post:
{body}"""
title = generate(model,tokenizer=tokeniser,prompt=title_prompt, verbose=False).strip()
body = body_match.group(1).strip() if body_match else raw_output
print("=== Final Title ===")
print(title)
print("\n=== Final Body ===")
print(body)
print("==================\n")
#browser automation stuff
from playwright.sync_api import sync_playwright
playwright_context = sync_playwright().start()
browser = playwright_context.chromium.connect_over_cdp("http://localhost:9222")
page = browser.contexts[0].pages[0] if browser.contexts[0].pages else browser.contexts[0].new_page()
toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=browser)
tools = toolkit.get_tools()
llm = ChatOllama(
model="qwen2.5:7b",
temperature=0.2,
)
# Agent prompt
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful browser automation assistant. Follow the instructions carefully and use the available tools."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Change these values
SUBREDDIT = "GetStudying"
task = f"""
1. Go to old.reddit.com
2. Make sure I am logged in. If not, wait for me to log in manually.
3. After I am logged in, go to https://old.reddit.com/r/{SUBREDDIT}/submit
4. In the title field, type: {title}
5. In the body/text field, paste this exact text:
{body}
6. Click the Post button.
"""
print("Running agent... (you may need to log in manually when the browser opens)")
agent_executor.invoke({"input": task})
print("\nDone!")
typical terminal output :
(web-assist) ➜ web_assist git:(main) ✗ uv run main.py
/Users/aradhyabaliyan/code/internship/web_assist/main.py:7: DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-community/issues/674 for details and migrationguidance toward standalone integration packages.
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
Fetching 9 files: 100%|███████████████████████████| 9/9 [00:00<00:00, 2136.80it/s]
Reconstruction complete: | | 0.00B / 0.00B
Download complete: | 0.00B
=== Final Title ===
Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration
=== Final Body ===
Ugh, I feel so dumb sometimes. I set a 50-minute pomodoro timer and play some background noises, but I still can't focus. I end up rewinding the same YouTube lecturelike 15 times, and it's driving me crazy. I thought the background noise would help, but it just makes me feel more lost. I end up watching the same video over and over, and it's like I'm stuck in a never-ending loop. Why does this happen to me? Isit possible to break this cycle? Anyone else out there feeling like this? #studystruggles #pomodorofailure #rewindloop
==================
python3(19720) MallocStackLogging: can't turn off malloc stack logging because it was not enabled.
Running agent... (you may need to log in manually when the browser opens)
> Entering new AgentExecutor chain...
Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com'}`
Navigating to https://www.reddit.com returned status code 200
Invoking: `click_element` with `{'selector': 'a[href="/r/login"]'}`
Unable to click on element 'a[href="/r/login"]'
Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}`
responded: It seems that you are already logged in or the login link is not present. Let's proceed to the next step and navigate to the submission page.
Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200
Invoking: `click_element` with `{'selector': 'input[name="title"]'}`
responded: Great, we are now on the submission page for the subreddit r/GetStudying. Let's fill out the form.
Unable to click on element 'input[name="title"]'
Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text']"}`
responded: It appears that the title input field is not immediately available or requires some additional interaction. Let's try to find and fill in the title field step by step.
First, let's retrieve the elements for the title field and any other relevant fields.
[]
Invoking: `get_elements` with `{'selector': 'input'}`
responded: It seems there are no elements matching the selector for the title field. Let's try a more general approach to find the title input field.
Let's retrieve all input fields and see if we can identify the title field.
[]
Invoking: `get_elements` with `{'selector': 'input[name="title"]'}`
responded: It seems that there are no input fields matching the general selector. Let's try a more specific approach to find the title field by using a more targetedCSS selector.
Let's retrieve elements with the CSS selector `input[name="title"]` again.
[]
Invoking: `get_elements` with `{'selector': 'input'}`
responded: It appears that the title input field is not being found using the `input[name="title"]` selector. Let's try a different approach to locate the title field.
We will retrieve all input fields and inspect their attributes to identify the title field.
[]
Invoking: `previous_webpage` with `{}`
responded: It seems that there are no input fields present on the page at this moment. Let's try navigating back to the previous page and then attempt to find the title field again.
Let's navigate back to the previous page.
Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200
Invoking: `get_elements` with `{'selector': 'input'}`
[]
Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}`
responded: It seems that there are still no input fields present on the page. Let's try navigating back to the submission page and then attempt to find the title field again.
Let's navigate back to the submission page.
Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200
Invoking: `get_elements` with `{'selector': 'input'}`
[]
Invoking: `previous_webpage` with `{}`
responded: It appears that there are still no input fields present on the page. This could be due to the page not fully loading or the elements not being available yet. Let's try waiting a bit and then attempting to find the title field again.
Let's wait for 5 seconds and then try again.
Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200
Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}`
responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait.
Let's navigate back to the submission page and wait for 5 seconds.
Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200
Invoking: `previous_webpage` with `{}`
responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait.
Let's navigate back to the submission page and wait for 5 seconds.
Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200
Invoking: `click_element` with `{'selector': "input[name='title']"}`
responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait.
Let's navigate back to the submission page and wait for 5 seconds.
Unable to click on element 'input[name='title']'
Invoking: `click_element` with `{'element': "input[name='title']", 'text': 'Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration'}`
responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait.
Let's navigate back to the submission page and wait for 5 seconds.
Traceback (most recent call last):
File "/Users/aradhyabaliyan/code/internship/web_assist/main.py", line 108, in <module>
agent_executor.invoke({"input": task})
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/chains/base.py", line 167, in invoke
self._call(inputs, run_manager=run_manager)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1590, in _call
next_step_output = self._take_next_step(
name_to_tool_map,
...<3 lines>...
run_manager=run_manager,
)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1290, in _take_next_step
list(
~~~~^
self._iter_next_step(
^^^^^^^^^^^^^^^^^^^^^
...<5 lines>...
),
^^
),
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1373, in _iter_next_step
yield self._perform_agent_action(
~~~~~~~~~~~~~~~~~~~~~~~~~~^
name_to_tool_map,
^^^^^^^^^^^^^^^^^
...<2 lines>...
run_manager,
^^^^^^^^^^^^
)
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1398, in _perform_agent_action
observation = tool.run(
agent_action.tool_input,
...<3 lines>...
**tool_run_kwargs,
)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1132, in run
raise error_to_raise
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1091, in run
tool_args, tool_kwargs = self._to_args_and_kwargs(
~~~~~~~~~~~~~~~~~~~~~~~~^
tool_input, tool_call_id
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 993, in _to_args_and_kwargs
tool_input = self._parse_input(tool_input, tool_call_id)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 834, in _parse_input
result_v2 = input_args.model_validate(tool_input)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/pydantic/main.py", line 790, in model_validate
return cls.__pydantic_validator__.validate_python(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
obj,
^^^^
...<5 lines>...
by_name=by_name,
^^^^^^^^^^^^^^^^
)
^
pydantic_core._pydantic_core.ValidationError: 1 validation error for ClickToolInput
selector
Field required [type=missing, input_value={'element': "input[name='...d PomodoroFrustration'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.14/v/missing
(web-assist) ➜ web_assist git:(main) ✗ uv run main.py
/Users/aradhyabaliyan/code/internship/web_assist/main.py:7: DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-community/issues/674 for details and migrationguidance toward standalone integration packages.
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
Fetching 9 files: 100%|███████████████████████████| 9/9 [00:00<00:00, 1666.10it/s]
Reconstruction complete: | | 0.00B / 0.00B
Download complete: | 0.00B
=== Final Title ===
Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration
=== Final Body ===
Ugh, I feel so dumb sometimes. I set a 50-minute pomodoro timer and play some background noises, but I still can't focus. I end up rewinding the same YouTube lecturelike 15 times, and it's driving me crazy. I thought the background noise would help, but it just makes me feel more lost. I end up watching the same video over and over, and it's like I'm stuck in a never-ending loop. Why does this happen to me? Isit possible to break this cycle? Anyone else out there feeling like this? #studystruggles #pomodorofailure #rewindloop
==================
Running agent... (you may need to log in manually when the browser opens)
> Entering new AgentExecutor chain...
Invoking: `navigate_browser` with `{'url': 'https://old.reddit.com'}`
Navigating to https://old.reddit.com returned status code 200
Invoking: `navigate_browser` with `{'url': 'https://old.reddit.com/r/GetStudying/submit'}`
Navigating to https://old.reddit.com/r/GetStudying/submit returned status code 200
Invoking: `click_element` with `{'selector': "a[href='/r/GetStudying/submit']"}`
Unable to click on element 'a[href='/r/GetStudying/submit']'
Invoking: `click_element` with `{'selector': "input[name='title']"}`
Unable to click on element 'input[name='title']'
Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text'], button[type='submit']"}`
responded: It seems there was an issue with clicking the elements directly. Let's try a different approach to ensure we are interacting with the correct elements on the page. We will first extract the text fields and buttons to ensure we are targeting the right ones.
Let's start by extracting the text fields and the post button.
[{"innerText": "save"}, {"innerText": "submit"}]
Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text'], button:contains('submit')"}`
Traceback (most recent call last):
File "/Users/aradhyabaliyan/code/internship/web_assist/main.py", line 108, in <module>
agent_executor.invoke({"input": task})
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/chains/base.py", line 167, in invoke
self._call(inputs, run_manager=run_manager)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1590, in _call
next_step_output = self._take_next_step(
name_to_tool_map,
...<3 lines>...
run_manager=run_manager,
)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1290, in _take_next_step
list(
~~~~^
self._iter_next_step(
^^^^^^^^^^^^^^^^^^^^^
...<5 lines>...
),
^^
),
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1373, in _iter_next_step
yield self._perform_agent_action(
~~~~~~~~~~~~~~~~~~~~~~~~~~^
name_to_tool_map,
^^^^^^^^^^^^^^^^^
...<2 lines>...
run_manager,
^^^^^^^^^^^^
)
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1398, in _perform_agent_action
observation = tool.run(
agent_action.tool_input,
...<3 lines>...
**tool_run_kwargs,
)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1132, in run
raise error_to_raise
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1098, in run
response = context.run(self._run, *tool_args, **tool_kwargs)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_community/tools/playwright/get_elements.py", line 96, in _run
results = _get_elements(page, selector, attributes)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_community/tools/playwright/get_elements.py", line 60, in _get_elements
elements = page.query_selector_all(selector)
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/sync_api/_generated.py", line 19843, in query_selector_all
self._sync(self._impl_obj.query_selector_all(selector=selector))
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_sync_base.py", line 111, in _sync
return task.result()
~~~~~~~~~~~^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_page.py", line 422, in query_selector_all
return await self._main_frame.query_selector_all(selector)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_frame.py", line 381, in query_selector_all
await self._channel.send(
^^^^^^^^^^^^^^^^^^^^^^^^^
"querySelectorAll", None, dict(selector=selector)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
),
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_connection.py", line 78, in send
return await self._connection.wrap_api_call(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<3 lines>...
)
^
File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_connection.py", line 636, in wrap_api_call
raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
playwright._impl._errors.Error: Page.query_selector_all: SyntaxError: Failed to execute 'querySelectorAll' on 'Document': 'button:contains("submit")' is not a valid selector.
at query (<anonymous>:5817:41)
at <anonymous>:5827:7
at SelectorEvaluatorImpl._cached (<anonymous>:5604:20)
at SelectorEvaluatorImpl._queryCSS (<anonymous>:5814:17)
at SelectorEvaluatorImpl._querySimple (<anonymous>:5694:19)
at <anonymous>:5642:29
at SelectorEvaluatorImpl._cached (<anonymous>:5604:20)
at SelectorEvaluatorImpl.query (<anonymous>:5635:19)
at Object.query (<anonymous>:5849:44)
at <anonymous>:5807:21
(web-assist) ➜ web_assist git:(main) ✗
im running this in Mac M1 Air (8GB RAM) with Golden Gate.
Suggestions are welcome !
1 comment
Mentions
My browser automation agent is stuck in an endless loop and throwing Pydantic errors, how do I actually fix this? So I tried setting up a LangChain Playwright agent to automate posting to Reddit, and man... it is driving me insane. It starts off fine, navigates to the links, but then it completely loses its mind trying to find input fields. It just keeps looping—retrying get_elements, going back and forth between pages, waiting for 5 seconds over and over again, and getting []every single time. And then to top it all off, it finally crashes completely with a Pydantic validation error (Field required [type=missing, input_value={'element': ...}) because it tried passing weird keys into the click tool instead of a simple CSS selector. Has anyone actually gotten LangChain's tool-calling agent with Playwright to work reliably on modern dynamic sites like Reddit? Or am I missing something obvious about how it handles selectors and page states? Any advice from people who've dealt with this would be awesome. code : from mlx_lm import load, generate import os from dotenv import load_dotenv from langchain.agents import create_agent, AgentState from langchain.messages import HumanMessage, ToolMessage, AIMessage from langchain_core.prompts import PromptTemplate from langchain_community.agent_toolkits import PlayWrightBrowserToolkit from langchain_community.tools.playwright.utils import create_sync_playwright_browser from langchain_core.prompts import ChatPromptTemplate from langchain_ollama import ChatOllama import re from langchain_classic.agents import AgentExecutor, create_tool_calling_agent load_dotenv() model, tokeniser = load("mlx-community/Qwen2.5-7B-Instruct-4bit") system_prompt = """You are a human writing assistant that rewrites a user's raw thoughts into a natural Reddit post. Strict rules you must follow: - Stay extremely faithful to the original message. Do not invent new problems, new details, new causes, or new solutions. - Do not add advice, diagnoses, or explanations the user did not already express. - Keep every important point the user made (the rewinding, the 50-minute pomodoro, the background noise, the feeling of being lost, the endless loop, the self-frustration). - Only elaborate and expand what is already there so it flows like a real person talking. - Make it sound completely human: messy, conversational, slightly self-deprecating, frustrated, and natural. Use contractions, incomplete thoughts, and everyday language. - Avoid any robotic phrasing, corporate words, lists, bullet points, or overly clean structure. - Avoid the use of emojis. - The final post must feel like it was written by the same person who wrote the original message, just cleaned up and expanded a little for Reddit. Output format (very important): Title: <short natural title> Body: <the full reddit post> """ query = '''I m so dumb in studies. i keeps on rewinding the same youtube lecture like 15 times even after setting a pomodoro of 50 minutes and playing background noises. but still i can't seem to focus and ends up being lost once again just to find that i need to watch this same video again. and this loops keeps going. Why does it happen ? and is it possible to stop this ?''' prompt = f"{system_prompt}\n\nUser's raw thoughts: {query}\n\nGenerated Reddit Post:" raw_output = generate( model, tokenizer= tokeniser, prompt=prompt, verbose=False, ) title_match = re.search(r"Title:\s*(.+)", raw_output) body_match = re.search(r"Body:\s*(.*?)(?=\nTitle:|$)", raw_output, re.DOTALL) if title_match: title = title_match.group(1).strip() else: title_prompt = f"""Generate only a short, natural, human-sounding Reddit title for this post. Do not use quotes. Do not write anything else. Just the title. Post: {body}""" title = generate(model,tokenizer=tokeniser,prompt=title_prompt, verbose=False).strip() body = body_match.group(1).strip() if body_match else raw_output print("=== Final Title ===") print(title) print("\n=== Final Body ===") print(body) print("==================\n") #browser automation stuff from playwright.sync_api import sync_playwright playwright_context = sync_playwright().start() browser = playwright_context.chromium.connect_over_cdp("http://localhost:9222") page = browser.contexts[0].pages[0] if browser.contexts[0].pages else browser.contexts[0].new_page() toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=browser) tools = toolkit.get_tools() llm = ChatOllama( model="qwen2.5:7b", temperature=0.2, ) # Agent prompt prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful browser automation assistant. Follow the instructions carefully and use the available tools."), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ]) agent = create_tool_calling_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) # Change these values SUBREDDIT = "GetStudying" task = f""" 1. Go to old.reddit.com 2. Make sure I am logged in. If not, wait for me to log in manually. 3. After I am logged in, go to https://old.reddit.com/r/{SUBREDDIT}/submit 4. In the title field, type: {title} 5. In the body/text field, paste this exact text: {body} 6. Click the Post button. """ print("Running agent... (you may need to log in manually when the browser opens)") agent_executor.invoke({"input": task}) print("\nDone!") typical terminal output : (web-assist) ➜ web_assist git:(main) ✗ uv run main.py /Users/aradhyabaliyan/code/internship/web_assist/main.py:7: DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-community/issues/674 for details and migrationguidance toward standalone integration packages. from langchain_community.agent_toolkits import PlayWrightBrowserToolkit Fetching 9 files: 100%|███████████████████████████| 9/9 [00:00<00:00, 2136.80it/s] Reconstruction complete: | | 0.00B / 0.00B Download complete: | 0.00B === Final Title === Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration === Final Body === Ugh, I feel so dumb sometimes. I set a 50-minute pomodoro timer and play some background noises, but I still can't focus. I end up rewinding the same YouTube lecturelike 15 times, and it's driving me crazy. I thought the background noise would help, but it just makes me feel more lost. I end up watching the same video over and over, and it's like I'm stuck in a never-ending loop. Why does this happen to me? Isit possible to break this cycle? Anyone else out there feeling like this? #studystruggles #pomodorofailure #rewindloop ================== python3(19720) MallocStackLogging: can't turn off malloc stack logging because it was not enabled. Running agent... (you may need to log in manually when the browser opens) > Entering new AgentExecutor chain... Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com'}` Navigating to https://www.reddit.com returned status code 200 Invoking: `click_element` with `{'selector': 'a[href="/r/login"]'}` Unable to click on element 'a[href="/r/login"]' Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}` responded: It seems that you are already logged in or the login link is not present. Let's proceed to the next step and navigate to the submission page. Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200 Invoking: `click_element` with `{'selector': 'input[name="title"]'}` responded: Great, we are now on the submission page for the subreddit r/GetStudying. Let's fill out the form. Unable to click on element 'input[name="title"]' Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text']"}` responded: It appears that the title input field is not immediately available or requires some additional interaction. Let's try to find and fill in the title field step by step. First, let's retrieve the elements for the title field and any other relevant fields. [] Invoking: `get_elements` with `{'selector': 'input'}` responded: It seems there are no elements matching the selector for the title field. Let's try a more general approach to find the title input field. Let's retrieve all input fields and see if we can identify the title field. [] Invoking: `get_elements` with `{'selector': 'input[name="title"]'}` responded: It seems that there are no input fields matching the general selector. Let's try a more specific approach to find the title field by using a more targetedCSS selector. Let's retrieve elements with the CSS selector `input[name="title"]` again. [] Invoking: `get_elements` with `{'selector': 'input'}` responded: It appears that the title input field is not being found using the `input[name="title"]` selector. Let's try a different approach to locate the title field. We will retrieve all input fields and inspect their attributes to identify the title field. [] Invoking: `previous_webpage` with `{}` responded: It seems that there are no input fields present on the page at this moment. Let's try navigating back to the previous page and then attempt to find the title field again. Let's navigate back to the previous page. Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200 Invoking: `get_elements` with `{'selector': 'input'}` [] Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}` responded: It seems that there are still no input fields present on the page. Let's try navigating back to the submission page and then attempt to find the title field again. Let's navigate back to the submission page. Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200 Invoking: `get_elements` with `{'selector': 'input'}` [] Invoking: `previous_webpage` with `{}` responded: It appears that there are still no input fields present on the page. This could be due to the page not fully loading or the elements not being available yet. Let's try waiting a bit and then attempting to find the title field again. Let's wait for 5 seconds and then try again. Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200 Invoking: `navigate_browser` with `{'url': 'https://www.reddit.com/r/GetStudying/submit'}` responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait. Let's navigate back to the submission page and wait for 5 seconds. Navigating to https://www.reddit.com/r/GetStudying/submit returned status code 200 Invoking: `previous_webpage` with `{}` responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait. Let's navigate back to the submission page and wait for 5 seconds. Navigated back to the previous page with URL 'https://www.reddit.com/'. Status code 200 Invoking: `click_element` with `{'selector': "input[name='title']"}` responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait. Let's navigate back to the submission page and wait for 5 seconds. Unable to click on element 'input[name='title']' Invoking: `click_element` with `{'element': "input[name='title']", 'text': 'Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration'}` responded: It seems that the page is still not loading the necessary elements. Let's try navigating back to the submission page and then attempt to find the title field again after a short wait. Let's navigate back to the submission page and wait for 5 seconds. Traceback (most recent call last): File "/Users/aradhyabaliyan/code/internship/web_assist/main.py", line 108, in <module> agent_executor.invoke({"input": task}) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/chains/base.py", line 167, in invoke self._call(inputs, run_manager=run_manager) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1590, in _call next_step_output = self._take_next_step( name_to_tool_map, ...<3 lines>... run_manager=run_manager, ) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1290, in _take_next_step list( ~~~~^ self._iter_next_step( ^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... ), ^^ ), ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1373, in _iter_next_step yield self._perform_agent_action( ~~~~~~~~~~~~~~~~~~~~~~~~~~^ name_to_tool_map, ^^^^^^^^^^^^^^^^^ ...<2 lines>... run_manager, ^^^^^^^^^^^^ ) ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1398, in _perform_agent_action observation = tool.run( agent_action.tool_input, ...<3 lines>... **tool_run_kwargs, ) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1132, in run raise error_to_raise File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1091, in run tool_args, tool_kwargs = self._to_args_and_kwargs( ~~~~~~~~~~~~~~~~~~~~~~~~^ tool_input, tool_call_id ^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 993, in _to_args_and_kwargs tool_input = self._parse_input(tool_input, tool_call_id) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 834, in _parse_input result_v2 = input_args.model_validate(tool_input) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/pydantic/main.py", line 790, in model_validate return cls.__pydantic_validator__.validate_python( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ obj, ^^^^ ...<5 lines>... by_name=by_name, ^^^^^^^^^^^^^^^^ ) ^ pydantic_core._pydantic_core.ValidationError: 1 validation error for ClickToolInput selector Field required [type=missing, input_value={'element': "input[name='...d PomodoroFrustration'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.14/v/missing (web-assist) ➜ web_assist git:(main) ✗ uv run main.py /Users/aradhyabaliyan/code/internship/web_assist/main.py:7: DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-community/issues/674 for details and migrationguidance toward standalone integration packages. from langchain_community.agent_toolkits import PlayWrightBrowserToolkit Fetching 9 files: 100%|███████████████████████████| 9/9 [00:00<00:00, 1666.10it/s] Reconstruction complete: | | 0.00B / 0.00B Download complete: | 0.00B === Final Title === Feeling Lost in Studies: Rewind Loop and Pomodoro Frustration === Final Body === Ugh, I feel so dumb sometimes. I set a 50-minute pomodoro timer and play some background noises, but I still can't focus. I end up rewinding the same YouTube lecturelike 15 times, and it's driving me crazy. I thought the background noise would help, but it just makes me feel more lost. I end up watching the same video over and over, and it's like I'm stuck in a never-ending loop. Why does this happen to me? Isit possible to break this cycle? Anyone else out there feeling like this? #studystruggles #pomodorofailure #rewindloop ================== Running agent... (you may need to log in manually when the browser opens) > Entering new AgentExecutor chain... Invoking: `navigate_browser` with `{'url': 'https://old.reddit.com'}` Navigating to https://old.reddit.com returned status code 200 Invoking: `navigate_browser` with `{'url': 'https://old.reddit.com/r/GetStudying/submit'}` Navigating to https://old.reddit.com/r/GetStudying/submit returned status code 200 Invoking: `click_element` with `{'selector': "a[href='/r/GetStudying/submit']"}` Unable to click on element 'a[href='/r/GetStudying/submit']' Invoking: `click_element` with `{'selector': "input[name='title']"}` Unable to click on element 'input[name='title']' Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text'], button[type='submit']"}` responded: It seems there was an issue with clicking the elements directly. Let's try a different approach to ensure we are interacting with the correct elements on the page. We will first extract the text fields and buttons to ensure we are targeting the right ones. Let's start by extracting the text fields and the post button. [{"innerText": "save"}, {"innerText": "submit"}] Invoking: `get_elements` with `{'selector': "input[name='title'], textarea[name='text'], button:contains('submit')"}` Traceback (most recent call last): File "/Users/aradhyabaliyan/code/internship/web_assist/main.py", line 108, in <module> agent_executor.invoke({"input": task}) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/chains/base.py", line 167, in invoke self._call(inputs, run_manager=run_manager) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1590, in _call next_step_output = self._take_next_step( name_to_tool_map, ...<3 lines>... run_manager=run_manager, ) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1290, in _take_next_step list( ~~~~^ self._iter_next_step( ^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... ), ^^ ), ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1373, in _iter_next_step yield self._perform_agent_action( ~~~~~~~~~~~~~~~~~~~~~~~~~~^ name_to_tool_map, ^^^^^^^^^^^^^^^^^ ...<2 lines>... run_manager, ^^^^^^^^^^^^ ) ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_classic/agents/agent.py", line 1398, in _perform_agent_action observation = tool.run( agent_action.tool_input, ...<3 lines>... **tool_run_kwargs, ) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1132, in run raise error_to_raise File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_core/tools/base.py", line 1098, in run response = context.run(self._run, *tool_args, **tool_kwargs) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_community/tools/playwright/get_elements.py", line 96, in _run results = _get_elements(page, selector, attributes) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/langchain_community/tools/playwright/get_elements.py", line 60, in _get_elements elements = page.query_selector_all(selector) File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/sync_api/_generated.py", line 19843, in query_selector_all self._sync(self._impl_obj.query_selector_all(selector=selector)) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_sync_base.py", line 111, in _sync return task.result() ~~~~~~~~~~~^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_page.py", line 422, in query_selector_all return await self._main_frame.query_selector_all(selector) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_frame.py", line 381, in query_selector_all await self._channel.send( ^^^^^^^^^^^^^^^^^^^^^^^^^ "querySelectorAll", None, dict(selector=selector) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ), ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_connection.py", line 78, in send return await self._connection.wrap_api_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... ) ^ File "/Users/aradhyabaliyan/code/internship/web_assist/.venv/lib/python3.14/site-packages/playwright/_impl/_connection.py", line 636, in wrap_api_call raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None playwright._impl._errors.Error: Page.query_selector_all: SyntaxError: Failed to execute 'querySelectorAll' on 'Document': 'button:contains("submit")' is not a valid selector. at query (<anonymous>:5817:41) at <anonymous>:5827:7 at SelectorEvaluatorImpl._cached (<anonymous>:5604:20) at SelectorEvaluatorImpl._queryCSS (<anonymous>:5814:17) at SelectorEvaluatorImpl._querySimple (<anonymous>:5694:19) at <anonymous>:5642:29 at SelectorEvaluatorImpl._cached (<anonymous>:5604:20) at SelectorEvaluatorImpl.query (<anonymous>:5635:19) at Object.query (<anonymous>:5849:44) at <anonymous>:5807:21 (web-assist) ➜ web_assist git:(main) ✗ im running this in Mac M1 Air (8GB RAM) with Golden Gate. Suggestions are welcome !
How to make this python script faster and able to run? hey there, i m working on this python script: pastebin.com and the main issue i m facing right now is its not running and i have been stuck in the terminal since so long... i just want to ask how to make it faster so we can execute it. Output when i run this script : ```DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See langchain-community#674 for details and migration guidance toward standalone integration packages. from langchain_community.utilities import SQLDatabase.``` currently its stuck with this same terminal output. My pc specs : Mac Air M1, Golden Gate 28 what i have tried : Tried running on github codespaces but still not running but stuck with the same output.