Changing browser to edge
i was trying changing the browser to edge but it seems link not working
in the .env i have changed the paths to : CHROME_PATH=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe CHROME_USER_DATA=C:\Users\FadiN\AppData\Local\Microsoft\Edge\User Data
I run the code and check " use own browser " i got this error :
`Traceback (most recent call last):
File "D:\Ai\TwitterAGent\browser-use-webui\webui.py", line 174, in run_custom_agent
browser_context_ = await playwright.chromium.launch_persistent_context(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<13 lines>...
)
^
File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\playwright\async_api_generated.py", line 14681, in launch_persistent_context
await self._impl_obj.launch_persistent_context(
...<50 lines>...
)
File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\playwright_impl_browser_type.py", line 159, in launch_persistent_context
from_channel(await self._channel.send("launchPersistentContext", params)),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\playwright_impl_connection.py", line 61, in send
return await self._connection.wrap_api_call(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
)
^
File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\playwright_impl_connection.py", line 528, in wrap_api_call
raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
playwright._impl._errors.TargetClosedError: BrowserType.launch_persistent_context: Target page, context or browser has been closed
Browser logs:
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate,HttpsUpgrades,PaintHolding,ThirdPartyStoragePartitioning,LensOverlay,PlzDedicatedWorker --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --no-sandbox --user-data-dir=C:\Users\FadiN\AppData\Local\Microsoft\Edge\User Data --remote-debugging-pipe about:blank -
pid=38260
Traceback (most recent call last): File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\queueing.py", line 625, in process_events response = await route_utils.call_process_api( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... ) ^ File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\route_utils.py", line 322, in call_process_api output = await app.get_blocks().process_api( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<11 lines>... ) ^ File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\blocks.py", line 2045, in process_api result = await self.call_function( ^^^^^^^^^^^^^^^^^^^^^^^^^ ...<8 lines>... ) ^ File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\blocks.py", line 1590, in call_function prediction = await fn(*processed_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\FadiN\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\utils.py", line 837, in async_wrapper response = await f(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Ai\TwitterAGent\browser-use-webui\webui.py", line 85, in run_browser_agent final_result, errors, model_actions, model_thoughts = await run_custom_agent( ^^^^^^^^^^^^^^^^^^^^^^^ ...<11 lines>... ) ^ File "D:\Ai\TwitterAGent\browser-use-webui\webui.py", line 239, in run_custom_agent await browser.close() ^^^^^^^ UnboundLocalError: cannot access local variable 'browser' where it is not associated with a value`
any thought to fix this ?
do you solve the problem?
Playwright's TargetClosedError: This error usually happens when the browser context or browser closes unexpectedly during execution. UnboundLocalError: This error indicates that the browser variable is not properly initialized or associated with a value before it's accessed.
Here are some suggestions to help you debug and resolve these errors:
-
Ensure Playwright Supports Edge While Playwright can work with Edge (as Edge is based on Chromium), you need to ensure that the CHROME_PATH in your .env file is correctly pointing to Edge's executable. Your current path seems correct: CHROME_PATH=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe CHROME_USER_DATA=C:\Users\FadiN\AppData\Local\Microsoft\Edge\User Data
-
Handle Browser Context and Closing Issues
- The TargetClosedError could mean the browser crashes or closes for some reason. To debug this:- Remove any restrictive features you might have applied using flags (e.g., --no-sandbox, --disable-dev-shm-usage, etc.).
- Check if the Edge browser works as expected outside of Playwright (i.e., by manually launching it).
Make sure the browser variable is properly initialized before the close() method is called. Wrap browser-related logic in a try-except-finally block to ensure proper cleanup:
try: browser = await playwright.chromium.launch_persistent_context( user_data_dir="C:\Users\FadiN\AppData\Local\Microsoft\Edge\User Data", executable_path="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", headless=False ) # Your browser actions go here
except Exception as e: print(f"An error occurred: {e}")
finally: if browser: await browser.close()
- Check Compatibility with the launch_persistent_context Method
- launch_persistent_context requires proper user_data_dir.
- Ensure the directory exists and is not locked by another instance of Edge.
- If the method doesn't work as intended, try launch() (non-persistent) and see if it bypasses the issue:browser = await playwright.chromium.launch(headless=False)
-
Check for Resource Conflict If you run multiple instances of Edge or any other processes that might interfere with the user_data_dir, close them before running your script.
-
Upgrade Dependencies
- Ensure you are using the latest versions of Playwright and its dependencies (pip install --upgrade playwright).
- Run playwright install to ensure all required binaries are correctly installed.
use the new code and set up your edge binary path to browser_path in .env