From 902f330f327a8a9418de27cb97f09bc3bcb87fbe Mon Sep 17 00:00:00 2001 From: Aaron Heinen Date: Tue, 17 May 2016 08:26:15 -0600 Subject: [PATCH] Add findByCss method findByCss can be used to target elements that cannot be targetted using findByBody or findByNameOrId --- src/Extensions/Selenium.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Extensions/Selenium.php b/src/Extensions/Selenium.php index 5fd668d..b4e2a9b 100644 --- a/src/Extensions/Selenium.php +++ b/src/Extensions/Selenium.php @@ -92,7 +92,11 @@ public function click($name) try { $link = $this->findByBody($name)->click(); } catch (InvalidArgumentException $e) { - $link = $this->findByNameOrId($name)->click(); + try { + $link = $this->findByCss($name)->click(); + } catch (InvalidArgumentException $e) { + $link = $this->findByNameOrId($name)->click(); + } } $this->updateCurrentUrl(); @@ -141,6 +145,21 @@ protected function findByNameOrId($name, $element = '*') } } + /** + * Filter according to an element's css attribute + * For example ->click('a[href^="/item/'.$item->slug.'"]') + * + * @param string $selector + * @return Crawler + */ + protected function findByCss($selector){ + try{ + return $this->session->element('css selector', $selector); + } catch (NoSuchElement $e) { + throw new InvalidArgumentException('No Css element exists.'); + } + } + /** * Find an element by its "value" attribute. *