Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dom changes after the click + sleep/wait/timeout #458

Closed
Devloger opened this issue Dec 18, 2022 · 1 comment
Closed

Dom changes after the click + sleep/wait/timeout #458

Devloger opened this issue Dec 18, 2022 · 1 comment

Comments

@Devloger
Copy link

Devloger commented Dec 18, 2022

Having something like that

$elem = $page->dom()->querySelectorAll('.user-card__action');
foreach($elem as $e)
{
	$e->click();
	$content = $page->evaluate( 'document.documentElement.innerHTML' )->getReturnValue();
	var_dump(strpos($content, 'Hello there!'));
	//$elem2 = $page->dom()->querySelector('.messenger-ovl__close.im-close');
	//var_dump($elem2);
	//$elem2->click();
	sleep(1);
}

die;

It doesnt work, $elem2 evaluates to null.
I think because the first click $e->click(); changes the DOM. Hello there! is always absent since it can be seen only sometimes after the click $e->click();.
And the $elem2 is always null because the '.messenger-ovl__close.im-close' appears always but only after the first click. So it is not present in the beginning in the DOM. its a close button that appears after first click of any card.

three questions.
1.So the first click changes the DOM. How to update it/reload it/...? So I can find the '.messenger-ovl__close.im-close' and click it after the first click that wasnt present before the first click?
2.Am I doing some waiting right? With sleep(1);? I want the browser and the code to wait at least 1-2s after the clicking, if its too fast it doesnt work. Or is there better/proper way?
3.How to get the actual html after the first click? Cuz sometimes Hello there! is present but only after the first click $e->click(); and it seems like $content = $page->evaluate( 'document.documentElement.innerHTML' )->getReturnValue(); returns the innerHTML before the first click happens.

@enricodias
Copy link
Member

If you call Page::dom() again, the previous node ids will become stale. See my last reply in #469. I'll leave this one closed since it's the same issue. Normally I would close the newer issue as a duplicate, but in this case it has more replies and more useful info.

For waiting, you can use Utils::tryWithTimeout() to perform any check every N microseconds. The Page::waitUntilContainsElement() uses this to wait until a desired element appear in the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants