josh42
josh42

Reputation: 11

Setting up Chrome DevTools (Selenium 4) Using Remote WebDriver in Python

I've been trying to set up the use of the Chrome DevTools using Selenium 4 and Python. I've been able to get it to run locally (without any of the async stuff), but when I try to use the webdriver.Remote implementation, it crashes.

Here is an example from the Selenium docs: https://www.selenium.dev/de/documentation/support_packages/chrome_devtools/

Below is how I tried to run it.

import asyncio

from selenium import webdriver
import selenium.webdriver.common.devtools.v96 as devtools

async def geo_location_test():
    try:
        chrome_options = webdriver.ChromeOptions()
        driver = webdriver.Remote(
            command_executor='http://D5365900:4444/wd/hub',
            options=chrome_options
        )

        async with driver.bidi_connection() as session:
            cdp_session = session.session
            await cdp_session.execute(devtools.emulation.set_geolocation_override(latitude=41.8781,
                                                                                  longitude=-87.6298,
                                                                                  accuracy=100))
        driver.get("https://my-location.org/")
    finally:
        driver.quit()


async def main():
    await geo_location_test()

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

It runs up to the line async with driver.bidi_connection() as session: (session is established and Chrome browser opens). But the it crashes with the following trace.

Traceback (most recent call last):
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 54, in <module>
    asyncio.run(main())
  File "C:\Program Files\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 51, in main
    await geo_location_test()
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 40, in geo_location_test
    async with driver.bidi_connection() as session:
  File "C:\Program Files\Python310\lib\contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1576, in bidi_connection
    async with cdp.open_cdp(ws_url) as conn:
  File "C:\Program Files\Python310\lib\contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\common\bidi\cdp.py", line 457, in open_cdp
    async with trio.open_nursery() as nursery:
  File "C:\Program Files\Python310\lib\site-packages\trio\_core\_run.py", line 796, in __aenter__
    self._scope.__enter__()
  File "C:\Program Files\Python310\lib\site-packages\trio\_core\_ki.py", line 159, in wrapper
    return fn(*args, **kwargs)
  File "C:\Program Files\Python310\lib\site-packages\trio\_core\_run.py", line 449, in __enter__
    task = _core.current_task()
  File "C:\Program Files\Python310\lib\site-packages\trio\_core\_run.py", line 2285, in current_task
    raise RuntimeError("must be called from async context") from None
RuntimeError: must be called from async context

As you can see, I'm using Python 3.10. I also upgraded the Selenium bindings to 4.1.0 and am running a Selenium 4.0.0 Hub/Node config to automate Chrome 96.

Any ideas, what is the problem here? Am I handling the asynchronous coroutines incorrectly?

Any help is much appreciated!

Update After trying to run it with trio (as suggested in Henry Ashton-Martyn's comment), I get the following error.

Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\site-packages\trio\_highlevel_open_tcp_stream.py", line 332, in attempt_connect
    await sock.connect(sockaddr)
  File "C:\Program Files\Python310\lib\site-packages\trio\_socket.py", line 682, in connect
    raise OSError(err, "Error in connect: " + os.strerror(err))
OSError: [Errno 10049] Error in connect: Unknown error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 58, in <module>
    trio.run(main)
  File "C:\Program Files\Python310\lib\site-packages\trio\_core\_run.py", line 1932, in run
    raise runner.main_task_outcome.error
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 55, in main
    await geo_location_test()
  File "C:\Users\y04082\eclipse-workspace\WWI-Testautomation\TestScripts\Josh\async_sel_4.py", line 44, in geo_location_test
    async with driver.bidi_connection() as session:
  File "C:\Program Files\Python310\lib\contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1576, in bidi_connection
    async with cdp.open_cdp(ws_url) as conn:
  File "C:\Program Files\Python310\lib\contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\common\bidi\cdp.py", line 458, in open_cdp
    conn = await connect_cdp(nursery, url)
  File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\common\bidi\cdp.py", line 479, in connect_cdp
    ws = await connect_websocket_url(nursery, url,
  File "C:\Program Files\Python310\lib\site-packages\trio_websocket\_impl.py", line 262, in connect_websocket_url
    return await connect_websocket(nursery, host, port, resource,
  File "C:\Program Files\Python310\lib\site-packages\trio_websocket\_impl.py", line 171, in connect_websocket
    stream = await trio.open_tcp_stream(host, port)
  File "C:\Program Files\Python310\lib\site-packages\trio\_highlevel_open_tcp_stream.py", line 367, in open_tcp_stream
    raise OSError(msg) from trio.MultiError(oserrors)
OSError: all attempts to connect to 0.0.0.0:4444 failed

Upvotes: 1

Views: 2140

Answers (1)

Henry Ashton-Martyn
Henry Ashton-Martyn

Reputation: 1

It seems that selenium doesn't use python's builtin async framework but uses a package called trio as well. You should be able to fix this problem by changing this code from:

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

to:

if __name__ == "__main__":
    import trio
    trio.run(main)

Upvotes: 0

Related Questions