Search code examples
symfonydomcrawler

DomCrawler. How to click button? "Unable to navigate from a "button" tag."


I got form:

<button type="submit" id="dostawa_zapisz" name="dostawa[zapisz]">Zapisz</button>
<input type="hidden" id="dostawa__token" name="dostawa[_token]" value="foo">

and want to click it:

//        $link = $crawler->filter('button:contains("Zapisz")')->link();
//        $link = $crawler->filterXPath('//*[@id="dostawa_zapisz"]')->link();
        $link = $crawler->selectButton('dostawa[zapisz]')->link();
        $crawler = $client->click($link);

but it show :

"Unable to navigate from a "button" tag."

(Is there any documentation about navigation using DomCrawler other than this)


Solution

  • That's a submit button you have, and there's no link, so you'll need to 'press' it (or submit).

    I struggled with the Goutte Web Scraper and ended up using Mink to do functional tests. You can read my blog about that. It's got some useful info, and plus if you follow the Goutte Web Scraper link, there's a bit more info there.

    But I'm pointing out that the headless browsers don't always work well, especially if you plan to use Javascript.

    For your code, you probably need to do something like this if you plan to use that:

    $form = $crawler->selectButton('Zapisz')->form();
    

    And then fill in the form and submit it.