Skip to content

Commit

Permalink
Fix StaleElementException in setValue when input has gone away (#336)
Browse files Browse the repository at this point in the history
Fix StaleElementException in setValue when input has gone away
  • Loading branch information
Chekote authored Sep 5, 2021
1 parent b219a04 commit 394e6ad
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Behat\Mink\Selector\Xpath\Escaper;
use WebDriver\Element;
use WebDriver\Exception\NoSuchElement;
use WebDriver\Exception\StaleElementReference;
use WebDriver\Exception\UnknownCommand;
use WebDriver\Exception\UnknownError;
use WebDriver\Exception;
Expand Down Expand Up @@ -707,7 +708,15 @@ public function setValue($xpath, $value)
document.activeElement.blur();
}
JS;
$this->executeJsOnElement($element, $script);

// Cover case, when an element was removed from DOM after its value was
// changed (e.g. by a JavaScript of a SPA) and therefore can't be focused.
try {
$this->executeJsOnElement($element, $script);
} catch (StaleElementReference $e) {
// Do nothing because an element was already removed and therefore
// blurring is not needed.
}
}

/**
Expand Down

0 comments on commit 394e6ad

Please sign in to comment.