I'm currently creating a unit test to be implemented in a website using Selenium and Facebook's php-webdriver. I am trying to go through all hyperlinks and verify if they work correctly using the following code:
$this->webDriver->get($this->url);
$results = $this->webDriver->findElements(WebDriverBy::cssSelector('a'));
for($i = 0; $i < count($results); $i++)
{
$this->webDriver->get($results[$i]->getAttribute('href'));
echo $this->webDriver->getTitle() . "\n";
$this->webDriver->navigate()->back();
}
The problem that occurs is that after the first "get" the unit test exits with the following error:
ObsoleteElementWebDriverError on
#4 C:\xampp\htdocs\php-webdriver-demo-master\tests\GitHubTest.php(63): RemoteWebElement->getAttribute('href')
#5 [internal function]: GitHubTest->testSearch()
When I don't switch from page, for example, when I just echo the title throughout the whole loop it loops perfectly. But as soon as I add an action it stops working. I've tried implementing "wait" but without any luck, it still throws the same error. Also, the code works perfectly when just running it once and not looping.. I can't seem to figure it out and have been crunching for a few hours now.. do you have any ideas?
ObsleteElementWebDriverElementerror is similar to theStale Element Exception. As such when you make your list of links to visit, when you navigate back to your start page, you will be reloading the page. This will mean your list of element references will no longer be valid. You'll need to re-make your list at the start of your loop, as well as outside of it.