@@ -675,41 +675,33 @@ def are_fields_on_page(
675675
676676 label_elements = section .locator (".label" ).all ()
677677 label_texts = [label .inner_text () for label in label_elements ]
678-
679678 normalized_labels = [
680679 normalize_label (label ) for label in label_texts if normalize_label (label )
681680 ]
682681
682+ def label_matches (field_normalized : str , idx : int ) -> bool :
683+ if visible is True :
684+ return label_elements [idx ].is_visible ()
685+ if visible is False :
686+ field_container = label_elements [idx ].locator (
687+ "xpath=ancestor::*[contains(@class, 'row') or contains(@class, 'field')][1]"
688+ )
689+ is_container_visible = field_container .is_visible ()
690+ logging .info (
691+ f"Label visible: { label_elements [idx ].is_visible ()} , "
692+ f"Container visible: { is_container_visible } → Effective: { is_container_visible } "
693+ )
694+ return not is_container_visible
695+ return True # visibility doesn't matter
696+
683697 for field_name in field_names :
684698 field_normalized = normalize_label (field_name )
685699 match_found = False
686700
687701 for i , label in enumerate (normalized_labels ):
688- if field_normalized in label :
689- if visible is True :
690- if label_elements [i ].is_visible ():
691- match_found = True
692- break
693- elif visible is False :
694- # Get the closest visible container (e.g., the wrapping field row)
695- field_container = label_elements [i ].locator (
696- "xpath=ancestor::*[contains(@class, 'row') or contains(@class, 'field')][1]"
697- )
698-
699- is_container_visible = field_container .is_visible ()
700-
701- logging .info (
702- f"Label visible: { label_elements [i ].is_visible ()} , "
703- f"Container visible: { is_container_visible } → Effective: { is_container_visible } "
704- )
705-
706- if not is_container_visible :
707- match_found = True
708- break
709- else :
710- # visibility doesn't matter
711- match_found = True
712- break
702+ if field_normalized in label and label_matches (field_normalized , i ):
703+ match_found = True
704+ break
713705
714706 logging .info (
715707 f"Checking for field '{ field_name } ' (visible={ visible } ) → Match found: { match_found } "
0 commit comments