@volker (Volker) posted a new topic, subject "PhoenixLiveView.element does not find a text in an a-tag". It read:

   Hi there,
    
    in my liveview tests I have this assertion:
    [code] 
        assert index_live
                     |> element("#contract-#{contract.id} a", "delete #{contract.id}")
                     |> render_click()
        
    [/code]
    
    When I run my tests, I get this failing test, but the requested text is there:
    [code] 
        ** (ArgumentError) selector "#contract-5395 a" returned 3 elements but none matched the text filter "delete 5395":
        
        ...
                 <a class="..." data-confirm="Are you sure?" href="#" id="delete 5395" phx-click="delete" phx-value-id="5395"><svg> ... </svg></a>
        
    [/code]
    
    I tried to get other parts of the a tag, but failed too.
    
    What am I doing wrong?
    
    Thank you! I appreciate your help.
    
    Best  
    Volker

To which @LostKobrakai (Benjamin Milde) replied

    The text filter is for matching text within the html element, not attributes – like, `<a>text</a>`.
    
    Also just a heads up: Whitespaces are not allowed in the id attribute.

After which, @volker (Volker) added

    Thank you for the two nudges.
    
    The following assertion did the trick:
    [code] 
        assert index_live
                     |> element(~s{[id="delete-#{contract.id}"]})
                     |> render_click()
        
    [/code]
    
    For (my) future reference, the ~s sigil will escape the “” and the square brackets support the match for attributes like “ID”.


{'source': 'discourse', 'url': 'https://elixirforum.com/t/phoenixliveview-element-does-not-find-a-text-in-an-a-tag/50171', 'title': 'PhoenixLiveView.element does not find a text in an a-tag', 'views': 137, 'has_accepted_answer': True, 'word_count': 172, 'like_count': 1, 'reply_count': 1, 'participant_count': 2, 'posts': 3, 'tags': 'liveview phoenix', 'author': '@volker (Volker)', 'participants': '@volker (Volker), @LostKobrakai (Benjamin Milde)', 'image_url': [None], 'created_at': '2022-09-10T09:10:17.260Z', 'last_posted_at': '2022-09-10T10:27:27.796Z'}