multiple-amethyst
multiple-amethyst3y ago

How to get the image source?

How to get the image source of this element? page.locator('img'); This is not working: const image = await page.locator('img').src;
3 Replies
multiple-amethyst
multiple-amethystOP3y ago
Ok I found it const image = await page.locator('img').getAttribute('src');
adverse-sapphire
adverse-sapphire3y ago
As a tip, I'd recommend appending '[src]' to the end of your selector to make it extra safe (img[src]). What would happen if the <img> element doesn't have a src attribute? This image var would be undefined.
multiple-amethyst
multiple-amethystOP3y ago
Thanks for the tip!!!

Did you find this page helpful?