Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JorickPepin opened this issue Oct 9, 2024 · 3 comments
Open

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

JorickPepin opened this issue Oct 9, 2024 · 3 comments

Comments

@JorickPepin
Copy link

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

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

@stof
Copy link
Member

stof commented Apr 24, 2025

Please open a PR to update the documentation to document the correct behavior (which is the same behavior since years).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants