@@ -611,19 +611,34 @@ export const DOMBrowserRenderer: React.FC<RRWebDOMBrowserRendererProps> = ({
611
611
if ( ! isInCaptureMode && socket && snapshot ?. baseUrl ) {
612
612
const iframe = iframeRef . current ;
613
613
if ( iframe ) {
614
- const iframeRect = iframe . getBoundingClientRect ( ) ;
615
- const iframeX = lastMousePosition . x - iframeRect . left ;
616
- const iframeY = lastMousePosition . y - iframeRect . top ;
614
+ const focusedElement = iframeDoc . activeElement as HTMLElement ;
615
+ let coordinates = { x : 0 , y : 0 } ;
616
+
617
+ if ( focusedElement && focusedElement !== iframeDoc . body ) {
618
+ // Get coordinates from the focused element
619
+ const rect = focusedElement . getBoundingClientRect ( ) ;
620
+ coordinates = {
621
+ x : rect . left + rect . width / 2 ,
622
+ y : rect . top + rect . height / 2
623
+ } ;
624
+ } else {
625
+ // Fallback to last mouse position if no focused element
626
+ const iframeRect = iframe . getBoundingClientRect ( ) ;
627
+ coordinates = {
628
+ x : lastMousePosition . x - iframeRect . left ,
629
+ y : lastMousePosition . y - iframeRect . top
630
+ } ;
631
+ }
617
632
618
633
const selector = clientSelectorGenerator . generateSelector (
619
634
iframeDoc ,
620
- { x : iframeX , y : iframeY } ,
635
+ coordinates ,
621
636
ActionType . Keydown
622
637
) ;
623
638
624
639
const elementInfo = clientSelectorGenerator . getElementInformation (
625
640
iframeDoc ,
626
- { x : iframeX , y : iframeY } ,
641
+ coordinates ,
627
642
clientSelectorGenerator . getCurrentState ( ) . listSelector ,
628
643
clientSelectorGenerator . getCurrentState ( ) . getList
629
644
) ;
0 commit comments