Skip to content
New issue

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

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

Already on GitHub? # to your account

How to wait for all images to be loaded? #626

Open
nicodemuz opened this issue Feb 26, 2024 · 2 comments
Open

How to wait for all images to be loaded? #626

nicodemuz opened this issue Feb 26, 2024 · 2 comments

Comments

@nicodemuz
Copy link

I tried the following code, but it did not seem to work:

$client->waitForVisibility('img');
@nicodemuz
Copy link
Author

Hmmm, I'm not sure if it's cause my images have loading="lazy" attribute..

@nicodemuz
Copy link
Author

This seems to be a workaround:

$client->executeScript("
    Promise.all(Array.from(document.images).filter(img => !img.complete).map(img => new Promise(resolve => { img.onload = img.onerror = resolve; }))).then(() => {
        console.log('All images loaded.');
        var div = document.createElement('div');
        div.setAttribute('id', 'imagesLoaded');
        div.innerHTML = 'Images loaded';
        document.body.appendChild(div);
    });
");

$client->waitForVisibility('div#imagesLoaded');

You can then continue to remove the appended div if needed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant