phphtmlsymfonydomcrawler

How to select a link by its label containing non breaking spaces using the symfony dom crawler?


I have a link:

<a
    id="return-button"
    href="{{ path('user_list') }}"
    class="btn btn-secondary">
    <i class="icon ion-md-arrow-back"></i>&nbsp;Back to the list
</a>

I try to use it in a Test. For this purpose, I select it by its label:

self::$client->getCrawler()
             ->selectLink('Back to the list')
             ->link();

But the test throws an error:

InvalidArgumentException: The current node list is empty.

It's because of the &nbsp; in the link label. If I replace it with a simple space, the dow crawler is able to select it.

How can I select this link without removing the non breaking space in it?


Solution

  • As mentioned in this issue: https://github.com/symfony/symfony/issues/33062, this behavior is a bug due to the XPath engine used by Symfony.

    Currently, to avoid the problem, we can specify the non breaking space in the DOM crawler selector by doing:

    self::$client->getCrawler()
                 ->selectLink("\xc2\xa0Back to the list")
                 ->link();
    

    Mind the double quotes instead of the simple ones.