Skip to content

ERROR:asyncio:Task was destroyed but it is pending #3499

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
guocity opened this issue Feb 10, 2025 · 8 comments
Closed

ERROR:asyncio:Task was destroyed but it is pending #3499

guocity opened this issue Feb 10, 2025 · 8 comments
Labels
can't reproduce We tried to see what you saw, but didn't UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@guocity
Copy link

guocity commented Feb 10, 2025

seleniumbase/undetected/cdp_driver/connection.py:543

perhaps change the synchronous cancel() method on Listener into an async one?

Image
@guocity guocity changed the title ERROR:asyncio:Task was destroyed but it is pending! ERROR:asyncio:Task was destroyed but it is pending Feb 10, 2025
@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 Feb 10, 2025
@mdmintz
Copy link
Member

mdmintz commented Feb 10, 2025

I ran SeleniumBase/examples/cdp_mode/raw_xhr_async.py without issue.


Image

I also ran SeleniumBase/examples/cdp_mode/raw_xhr_sb.py without issue.


Image

Not sure how you got those asyncio issues. Make sure you're following the steps of the examples correctly.

@mdmintz mdmintz closed this as completed Feb 10, 2025
@guocity
Copy link
Author

guocity commented Feb 10, 2025

yea, I know sample code have no error, but if I pass in different host and port in the driver line 61, I got the error

@mdmintz
Copy link
Member

mdmintz commented Feb 10, 2025

There are examples for the correct way to set host and port (for taking control of an existing Chrome browser) here:
#3354 (comment)

@guocity
Copy link
Author

guocity commented Feb 10, 2025

I see the issue:
loop = asyncio.new_event_loop()
loop.run_until_complete(crawl()) //error
asyncio.run(crawl()) // no error

@mdmintz
Copy link
Member

mdmintz commented Feb 10, 2025

Depending on what you're trying to do, you would use asyncio.run() instead of loop.run_until_complete(). Eg:

import asyncio
from seleniumbase.undetected.cdp_driver import cdp_util

async def main():
    host = "127.0.0.1"
    port = 9222
    driver = await cdp_util.start_async(host=host, port=port)
    page = await driver.get("https://seleniumbase.io/")
    await asyncio.sleep(2)
    print(await page.evaluate("document.title"))
    element = await page.find("CDP Mode")
    await element.click_async()
    await asyncio.sleep(2)
    print(await page.evaluate("document.title"))

if __name__ == "__main__":
    asyncio.run(main())

(That example assumes you already have a browser open with that remote-debugging-port.)

@guocity
Copy link
Author

guocity commented Feb 10, 2025

yes, there is a browser opened,
I see cdp_driver/cdp_util have lot of function not implemented, for example, get_current_url(), are you going to make it on par with other driver?

@mdmintz
Copy link
Member

mdmintz commented Feb 10, 2025

get_current_url() exists in both the SB CDP Driver and regular SeleniumBase:

def get_current_url(self):

def get_current_url(self):


seleniumbase/undetected/cdp_driver/cdp_util.py is mainly from the nodriver fork in nodriver/core/util.py.
(It'll probably remain mostly unchanged for backwards compatibility.)

@guocity
Copy link
Author

guocity commented Feb 10, 2025

I see, use
sb = sb_cdp.CDPMethods(loop, page, driver)
to get all functionalities
It's a little hard to understand for beginner

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 UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

No branches or pull requests

2 participants