Skip to content

Commit e44c723

Browse files
committed
Optimize single element find
1 parent 19e5890 commit e44c723

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/Element/Element.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,21 @@ public function waitFor($timeout, $callback)
143143
*/
144144
public function find($selector, $locator)
145145
{
146-
$items = $this->findAll($selector, $locator);
146+
if ('named' === $selector) {
147+
$item = $this->find('named_exact', $locator);
148+
if (null === $item) {
149+
$item = $this->find('named_partial', $locator);
150+
}
151+
152+
return $item;
153+
}
154+
155+
$xpath = $this->selectorsHandler->selectorToXpath($selector, $locator);
156+
$xpathFirst = '(' . $xpath . ')[1]';
157+
158+
$items = $this->findAll('xpath', $xpathFirst);
147159

148-
return count($items) ? current($items) : null;
160+
return count($items) > 0 ? reset($items) : null;
149161
}
150162

151163
/**
@@ -155,7 +167,7 @@ public function findAll($selector, $locator)
155167
{
156168
if ('named' === $selector) {
157169
$items = $this->findAll('named_exact', $locator);
158-
if (empty($items)) {
170+
if (count($items) === 0) {
159171
$items = $this->findAll('named_partial', $locator);
160172
}
161173

0 commit comments

Comments
 (0)