Skip to content

[DomCrawler][BrowserKit] Unable to submit form with button value #58509

@JorickPepin

Description

@JorickPepin

Symfony version(s) affected

7.1

Description

The submitForm(string $button, ...) method indicates :

@param string $button The text content, id, value or name of the form <button> or <input type="submit">

However, the value attribute doesn't seem to be checked :

Symfony\Component\BrowserKit\Exception\InvalidArgumentException: There is no button with "btn_value" as its content, id, value or name.

How to reproduce

<form method="post">
    <input type="text" id="name" name="name" required>

    <button type="submit" id="btn_id" name="btn_name" value="btn_value">My button</button>
</form>
public function testFormButton(): void
{
    $client = static::createClient();

    $client->request('GET', '/');

    $client->submitForm('My button', ['name' => 'John']);
    $client->submitForm('btn_id', ['name' => 'John']);
    $client->submitForm('btn_name', ['name' => 'John']);
    $client->submitForm('btn_value', ['name' => 'John']); // this fails
}

Possible Solution

The XPath query behind this doesn't check the value attribute of <button> tags:

// Crawler.php

public function selectButton(string $value): static
{
    return $this->filterRelativeXPath(
        sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
    );
}

I can work on a PR if you confirm the issue. Thanks

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy