1717use Facebook \WebDriver \WebDriver ;
1818use Facebook \WebDriver \WebDriverBy ;
1919use Facebook \WebDriver \WebDriverElement ;
20+ use Symfony \Component \CssSelector \CssSelectorConverter ;
2021use Symfony \Component \DomCrawler \Crawler as BaseCrawler ;
2122use Symfony \Component \Panther \ExceptionThrower ;
2223
@@ -154,9 +155,15 @@ public function parents()
154155 /**
155156 * @see https://github.com/symfony/symfony/issues/26432
156157 */
157- public function children ()
158+ public function children (string $ selector = null )
158159 {
159- return $ this ->createSubCrawlerFromXpath ('child::* ' );
160+ $ xpath = 'child::* ' ;
161+ if (null !== $ selector ) {
162+ $ converter = $ this ->createCssSelectorConverter ();
163+ $ xpath = $ converter ->toXPath ($ selector , 'child:: ' );
164+ }
165+
166+ return $ this ->createSubCrawlerFromXpath ($ xpath );
160167 }
161168
162169 public function attr ($ attribute ): string
@@ -174,16 +181,32 @@ public function nodeName(): string
174181 return $ this ->getElementOrThrow ()->getTagName ();
175182 }
176183
177- public function text (): string
184+ public function text ($ default = null ): string
178185 {
179- return $ this ->getElementOrThrow ()->getText ();
186+ try {
187+ return $ this ->getElementOrThrow ()->getText ();
188+ } catch (\InvalidArgumentException $ e ) {
189+ if (null === $ default ) {
190+ throw $ e ;
191+ }
192+
193+ return (string ) $ default ;
194+ }
180195 }
181196
182- public function html (): string
197+ public function html ($ default = null ): string
183198 {
184- $ this ->getElementOrThrow ();
199+ try {
200+ $ this ->getElementOrThrow ();
201+
202+ return $ this ->attr ('outerHTML ' );
203+ } catch (\InvalidArgumentException $ e ) {
204+ if (null === $ default ) {
205+ throw $ e ;
206+ }
185207
186- return $ this ->attr ('outerHTML ' );
208+ return (string ) $ default ;
209+ }
187210 }
188211
189212 public function evaluate ($ xpath ): self
@@ -453,4 +476,16 @@ public function findElements(WebDriverBy $locator)
453476 {
454477 return $ this ->getElementOrThrow ()->findElements ($ locator );
455478 }
479+
480+ /**
481+ * @throws \LogicException If the CssSelector Component is not available
482+ */
483+ private function createCssSelectorConverter (): CssSelectorConverter
484+ {
485+ if (!class_exists (CssSelectorConverter::class)) {
486+ throw new \LogicException ('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead. ' );
487+ }
488+
489+ return new CssSelectorConverter ();
490+ }
456491}
0 commit comments