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

200 views
Skip to first unread message

Joshua Hähnel

unread,
Nov 25, 2021, 3:23:19 PM11/25/21
to Selenium Users
0

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!

Reply all
Reply to author
Forward
0 new messages