For example if i create a array and run foreach on it
<?php
$array = [1,2,3,4,5,6,7,8,9,10];
foreach($array as $number){
echo $number."<br>";
$array = ['a','b','c','d'];
}
?>
result :
1
2
3
4
5
6
7
8
9
10
and change set the array after echo out the numbers , array doesn't change or even if I add new item on array it will not change that result , I mean array will not change in foreach
foreach ....
echo $number."<br>";
$array[]='x';
...
result : same
but when I do something like that
$selected_category setted to a link;
$session = Helpers::mink($selected_category);
$page = $session->getPage();
$as = $page->find('css', 'div#seasons-list')->findAll('css','a');
$items = [];
foreach ($as as $value) {
$link = $value->getAttribute('href');
$session->visit($link);
$page = $session->getPage();
$episodes = $page->find('css','div#category-posts')->findAll('css','article.grid-box');
foreach ($episodes as $episode) {
/* line 97 */ $link = $episode->find('css','a')->getAttribute('href');
/* line 98 */ $session->visit($link);
$page = $session->getPage();
$items[] = $a= $page->findAll('css','span.embed-responsive-item')[0]->find('css','iframe')->getAttribute('src');
}
first gets the data that I expected and when I set new url on session on line 98 and when this returns i get this error Fatal error: Uncaught Error: Call to a member function getAttribute() on null ... line 97
that means $episodes object changed , but why , why it doesn't change on first foreach and it changes in second
Keep in mind that:
on every page reload/refresh the object is lost
Example: find element > refresh the page / navigate to similar page > do some action on the object element saved earlier in a variable => stale element error from selenium
make sure you are using the methods the right way
Example: find()
method will return an element object if the element is found OR it will return null
if the element is not found.
The second is your case, you are using getAttribute
on null
, you should handle this kind of error (fatal error in this case).
In order to handle it make sure the element is found, search for the element with find
then check if what returns is null
, if yes then throw an exception.