Skip to content

Crawler html is empty when retrieved from a function or class method #418

@monkeyArms

Description

@monkeyArms

I ran into what I consider a 'bizarre' issue the other day:

When a Symfony\Component\Panther\Client instance is used within the same calling function/method and the Symfony\Component\Panther\DomCrawler\Crawler instance is retrieved, everything works.

However, if the Crawler is retrieved from another function/method, Crawler::html() provides an empty string.

Example class:

<?php

namespace App\Test;

use Symfony\Component\Panther\Client;
use Symfony\Component\Panther\DomCrawler\Crawler;

class PantherTest
{
	/**
	 * @var string
	 */
	protected $url;

	
	public function __construct()
	{
		$this->url = 'https://example.com/';
	}
	
	/**
	 * @return Crawler
	 */
	protected function fetchUrlGetCrawler(): Crawler
	{
		$client = Client::createChromeClient();

		$client->request( 'GET', $this->url );

		return $client->getCrawler();
	}

	public function test1()
	{
		$client = Client::createChromeClient();

		$client->request( 'GET', $this->url );

		$crawler = $client->getCrawler();

		dump( $crawler->html() );
	}

	public function test2()
	{
		$crawler = $this->fetchUrlGetCrawler();

		dump( $crawler->html() );
	}
}

The PantherTest::test1 method works as expected:

$test = new PantherTest();
$test->test1();

but the PantherTest::test2 method does not, even though the exact same code is duplicated inside another method:

$test = new PantherTest();
$test->test2();

I've tried this on both my local dev server, and a remote debian/apache server with the same results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions