symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.94k stars 222 forks source link

Is there a way to click on input tag without getting a \LogicException #277

Closed MatthieuMan closed 4 years ago

MatthieuMan commented 4 years ago

Hi,

Is there a way to clic on any tag like <input> except <a> and do not get a LogicException: Unable to navigate from a "input" tag. ?

Thanks

webignition commented 4 years ago

Sounds like what you want to do should work.

Can you share a minimal example of your code that isn't working as expected?

MatthieuMan commented 4 years ago

Hi, There it is I'd like to click on a input tag which trigger an action in javascript.

  $link = $crawler
            ->filter('input#gaCookiesSwitchButton')
            ->link()
        ;
        $client->click($link);

but it seems that we cannot navigate from a "input" tag.

**
 * @author Kévin Dunglas <dunglas@gmail.com>
 */
final class Link extends BaseLink
{
    use ExceptionThrower;
    private $element;
    public function __construct(WebDriverElement $element)
    {
        $tagName = $element->getTagName();
        if ('a' !== $tagName && 'area' !== $tagName && 'link' !== $tagName) {
            throw new \LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName));
        }
        $this->element = $element;
        $this->method = 'GET';
    }

So is there another way to perfom an action on these tags?

Thanks a lot!!

domis86 commented 4 years ago

@MatthieuMan Indeed \Symfony\Component\Panther\Client::click requires argument of type \Symfony\Component\DomCrawler\Link - so seems not good for clicking input element.

Can you try maybe this instead? :

$client->getMouse()->clickTo($cssSelector);

See: \Symfony\Component\Panther\WebDriver\WebDriverMouse::clickTo

webignition commented 4 years ago

@MatthieuMan Using the crawler to get a WebDriverElement instance and clicking directly on that should suffice:

$inputCrawler = $crawler->filter('input#gaCookiesSwitchButton');
$inputElement = $inputCrawler->first();
$inputElement->click();

The above example is a perhaps a little verbose in order to be more clear.

MatthieuMan commented 4 years ago

@domis86

$client->getMouse()->clickTo($cssSelector);

works perfectly thank you

@webignition thanks, it works as well, that's said, more verbose yes.

Thank you guys !!