You can query a page for focusable elements with a CSS selector, and you will get a number that is nearly right and occasionally a lie. The browser's sequential focus navigation order is its own thing: it respects tabindex , inert subtrees, visibility , shadow DOM and the order the elements actually sit in. So instead of querying, I pressed the key.
await page . cdp ( "Input.dispatchKeyEvent" , { type : "rawKeyDown" , key : "Tab" , code : "Tab" , windowsVirtualKeyCode : 9 } ) ; await page . cdp ( "Input.dispatchKeyEvent" , { type : "keyUp" , key : "Tab" , code : "Tab" , windowsVirtualKeyCode : 9 } ) ; const where = await page . evaluate ( ( ) => document . activeElement . textContent . trim ( ) ) ;
Enter fullscreen mode
Exit fullscreen mode
That is a real Tab press, in a real Chrome, on the live site . Do it in a loop, read document.activeElement and its computed style after each one, and you have the keyboard experience of a page as a list of rows.…