google-chromeselenium-webdriverselenium-chromedrivergoogle-chrome-devtoolschrome-devtools-protocol

Chrome Selenium CDP Bidi API - Next Commands sended to Target Session have no effect while the initial one does work


Hello everyone I'm facing a big trouble trying to hook TargetCreated Event and sending CDP commands to every new target found.

I'm using the last Selenium & Chrome.

I start a bidi session and send my commands and it work as expected.

async with self.driver.bidi_connection() as connection:
    self.cdp_bidi_main_session, self.cdp_bidi_main_devtools, self.cdp_cdp = connection.session, connection.devtools, connection.cdp
    await self.cdp_bidi_main_session.execute(self.cdp_bidi_main_devtools.page.enable())
    await self.__inject_javascript_object_via_bidi(self.cdp_bidi_main_session, self.cdp_bidi_main_devtools)

The javascript properties are successfully overrided.

But then, while listening for the TargetCreated Event and receiving them successfully, attaching to the target and then initiating a session to send commands don't work as expected...

event = new_targetcreated_event_received
async with self.driver.bidi_connection_with_target(event.target_info.target_id, self.target_attached_counter) as connection: -> I have modified selenium bidi source to make us able choosing session target and incrementing command ids
    cdp_bidi_main_session, cdp_bidi_main_devtools, cdp_cdp, cdp_ws_url = connection.session, connection.devtools, connection.cdp, connection.ws_url
    await cdp_bidi_main_session.execute(cdp_bidi_main_devtools.page.enable())
    # await trio.sleep(2) -> no differences
    await self.__inject_javascript_object_via_bidi(cdp_bidi_main_session, cdp_bidi_main_devtools)

I have struggled to try many different things :

What I didn't tried :

Here is a sample output of the command sended, as you notice this should work as expected...

F054E949597158BF152714D31B7D2DBD
{"method": "Target.getTargets", "params": {}, "id": 0}
{"method": "Target.attachToTarget", "params": {"targetId": "F054E949597158BF152714D31B7D2DBD", "flatten": true}, "id": 1}
ws://localhost:9222/devtools/browser/ce75db14-2dd8-41b3-92d7-2125604f4154
{"method": "Page.enable", "id": 0, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'deviceMemory', {get: () => 32})"}, "id": 1, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'webdriver', {get: () => false})"}, "id": 2, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en','fr-FR', 'fr']})"}, "id": 3, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'language', {get: () => ''fr-FR'})"}, "id": 4, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
{"method": "Target.setDiscoverTargets", "params": {"discover": true}, "id": 5, "sessionId": "F9F2C2303E47C4F042BC46B789AEF1DF"}
mitm targets thread started
TargetCreated(target_info=TargetInfo(target_id=TargetID('28FECF0B602368C5FDC98F1CD7AA42B3'), type_='page', title='New Tab', url='chrome://newtab/', attached=False, can_access_opener=False, opener_id=None, opener_frame_id=None, browser_context_id=BrowserContextID('ECC6C0EB1E4D82FF193AF53D9EAE8486'), subtype=None))
{"method": "Target.attachToTarget", "params": {"targetId": "28FECF0B602368C5FDC98F1CD7AA42B3", "flatten": true}, "id": 2}
{"method": "Page.enable", "id": 0, "sessionId": "D967794A83E55D16C23734D11FF69DB6"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'deviceMemory', {get: () => 32})"}, "id": 1, "sessionId": "D967794A83E55D16C23734D11FF69DB6"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'webdriver', {get: () => false})"}, "id": 2, "sessionId": "D967794A83E55D16C23734D11FF69DB6"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en','fr-FR', 'fr']})"}, "id": 3, "sessionId": "D967794A83E55D16C23734D11FF69DB6"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'language', {get: () => ''fr-FR'})"}, "id": 4, "sessionId": "D967794A83E55D16C23734D11FF69DB6"}
TargetCreated(target_info=TargetInfo(target_id=TargetID('253AFEEF629E8CB2B201B2F78703AEE8'), type_='iframe', title='', url='', attached=False, can_access_opener=False, opener_id=None, opener_frame_id=None, browser_context_id=BrowserContextID('ECC6C0EB1E4D82FF193AF53D9EAE8486'), subtype=None))
{"method": "Target.attachToTarget", "params": {"targetId": "253AFEEF629E8CB2B201B2F78703AEE8", "flatten": true}, "id": 3}
{"method": "Page.enable", "id": 0, "sessionId": "101425CF703EC42579FC95DF071DB019"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'deviceMemory', {get: () => 32})"}, "id": 1, "sessionId": "101425CF703EC42579FC95DF071DB019"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'webdriver', {get: () => false})"}, "id": 2, "sessionId": "101425CF703EC42579FC95DF071DB019"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en','fr-FR', 'fr']})"}, "id": 3, "sessionId": "101425CF703EC42579FC95DF071DB019"}
{"method": "Page.addScriptToEvaluateOnNewDocument", "params": {"source": "Object.defineProperty(navigator, 'language', {get: () => ''fr-FR'})"}, "id": 4, "sessionId": "101425CF703EC42579FC95DF071DB019"}

P.S : I have modified the source code of selenium bidi so we can specify target session and command ids in session opening.


Solution

  • Okay I have found the answer.

    Sended commands are destroyed once the attached session does.

    So you have to keep a opened flattened session reference in a external Thread to keep your commands data.