3

How to update selected Dom element with DomCrawler component of symfony?

$crawler->filter("table.positions")->addHtmlContent("<tr class="position"></td></td></tr>");

$crawler->filter('.position')->html(); // InvalidArgumentException: The current node list is empty.
Ivan M
  • 330
  • 2
  • 9

2 Answers2

3

try this

$crawler->filter('.position')->getNode(0)->setAttribute('ATTR','VALUE');

this code get the first node matches the selector and change the value of the selected attribute.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
bara batta
  • 1,002
  • 8
  • 8
0

As per documentation:

All filter methods return a new Crawler instance with filtered content.

hence

$populatedCrawler = $crawler->filter("table.positions")->addHtmlContent("<tr class="position"></td></td></tr>");

echo '<pre>'.$populatedCrawler->filter('.position')->html().'</pre>';

will give you what you expect.

Strayobject
  • 642
  • 11
  • 20