Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I download images from Selenium WITHOUT another URL request?

I'm using Python, and I'm trying to render web pages with Selenium and download all of their images. The method I've read about is to save the src attribute URL values and then submit another HTTP request, like so:

import urllib.request

url = driver.find_element_by_id("your-image-id").get_attribute("src")
urllib.request.urlretrieve(url, "local-filename.jpg")

But this is really inefficient at scale, since I'd be submitting a ton of extra requests for images I already loaded (I tried benchmarking with this approach, and it was many times slower than Selenium's downloading and rendering, which was surprising)

I couldn't find a way of saving the images directly from Selenium, even though this seems like a pretty obvious feature they'd have. Is there a way of doing this? (I can switch away from Python if I absolutely need to.)

like image 430
saml Avatar asked Feb 01 '26 10:02

saml


1 Answers

The trick a browser uses is to download images asynchronously. Like download them all at once, to save waiting for the webpage to respond.

If you could offload the actual downloading like here Python - Example of urllib2 asynchronous / threaded request using HTTPS it would surely go a LOT quicker.

And as said, reading out the browser cache is tricky. Implementatie might change with the next browser version

like image 86
Taco Verhagen Avatar answered Feb 03 '26 23:02

Taco Verhagen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!