Skip to content

ValueError: The future belongs to a different loop when using CPD mode #3725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LiPingYen opened this issue May 3, 2025 · 7 comments
Closed
Labels
can't reproduce We tried to see what you saw, but didn't external Outside SeleniumBase's scope. / Ask somewhere else. UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode workaround exists You can reach your destination if you do this...

Comments

@LiPingYen
Copy link

LiPingYen commented May 3, 2025

Description:

When attempting to retrieve cookies in CDP mode using SeleniumBase, I encountered the following error:

ValueError: The future belongs to a different loop than the one specified as the loop argument

Reproducible Example:

from seleniumbase import SB
import nest_asyncio

nest_asyncio.apply()

with SB(uc=True, incognito=True, demo=False) as sb:
    sb.activate_cdp_mode("https://news.google.com/home?hl=en-US&gl=US&ceid=US:en")
    cookies = sb.cdp.get_all_cookies()

Expected Behavior:

Should return a list of cookies from the specified URL without throwing an exception.

Environment:

SeleniumBase version: 4.38.0
Python version: 3.12.10
OS: MacOS 15.4.1
Browser: Chrome 136.0.7103.49 (Official Build) (arm64)

Let me know if additional details or logs are needed. Thanks!

@mdmintz mdmintz added can't reproduce We tried to see what you saw, but didn't UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels May 3, 2025
@mdmintz
Copy link
Member

mdmintz commented May 3, 2025

I ran your example exactly as written, and I couldn't reproduce your issue.
It's quite possible you've done something outside your code, which is masked by using nest_asyncio.
Make sure issues you've filed contain the full code for reproducing the issue.

@mdmintz mdmintz closed this as completed May 3, 2025
@LiPingYen
Copy link
Author

Hi @mdmintz, the minimume reproducible code is what I mentioned.
The reason why I use nest_asyncio is because I ran the code in Jupyter notebook.

@mdmintz mdmintz added the external Outside SeleniumBase's scope. / Ask somewhere else. label May 3, 2025
@mdmintz
Copy link
Member

mdmintz commented May 3, 2025

Likely an issue with Jupyter Notebooks, which aren't an officially supported environment.

Jupyter Notebooks have custom event loop code (specifically the event loop policy of the kernel), which is problematic for frameworks that use asyncio.
You could try https://stackoverflow.com/a/76614024/7058266.

@LiPingYen
Copy link
Author

Thanks @mdmintz, I will test the solution.

@mdmintz
Copy link
Member

mdmintz commented May 3, 2025

You could also try using the SeleniumBase async API: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md#sb_sf_24

Eg: examples/cdp_mode/raw_async.py

And maybe this format will work too: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md#sb_sf_25

Eg: examples/cdp_mode/raw_cdp.py

@LiPingYen
Copy link
Author

LiPingYen commented May 3, 2025

I tested SB CDP Sync API. No Selenium format, and that worked for me.
Just wonder why SB context manager format doesn't not work.

@mdmintz mdmintz added the workaround exists You can reach your destination if you do this... label May 3, 2025
@mdmintz
Copy link
Member

mdmintz commented May 3, 2025

It's likely due to the same Jupyter Notebooks bug affecting other frameworks.
At least a workaround exists until Jupyter Notebooks fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce We tried to see what you saw, but didn't external Outside SeleniumBase's scope. / Ask somewhere else. UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode workaround exists You can reach your destination if you do this...
Projects
None yet
Development

No branches or pull requests

2 participants