1

I am using the Selenium framework with Python, I am trying to save bandwidth while doing automation by reusing the images preloaded in the Chrome webdriver in my Python code. I am able to access the source url of the images, so far I am just re-donwloading them again with requests. I know for certain that normal browsers save images to temporary location on hard drive, and I guess Chrome webdriver does the same. How do I access these files with Selenium given that I know the src?

drerD
  • 629
  • 1
  • 9
  • 24

1 Answers1

2

You should reuse your chrome profile, something like that:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\path\\to\\your\\profile")  # Path to your chrome profile
driver = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

About the path, this can be done by going to chrome://version/ in your chrome, one of the settings there is your path, this will reuse your chrome profile, you might need to remove the last directory from the path if it's Default directory.

If you want your own profile that is not connected to your chrome, you can specify any path you want, and selenium will create it for you.

Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • Thanks, I am able to locate images in profile/Default/Cache, however images are renamed with file extensions removed from file names. The new names don't correspond to the original names from the source file, or follow any kind of pattern. I also try to see if the creation date/time of the temporary files correspond to any pattern, not really. – drerD Jun 21 '17 at 19:03
  • Right chrome caches the images and rename them, that because if if page Y has an image `a.jpg`, and page X has an image `a.jpg` might be an issue, so chrome hashes the images name to prevent this. BTW if images aren't important at all, [disable them](https://stackoverflow.com/questions/28070315/python-disable-images-in-selenium-google-chromedriver)! – Or Duan Jun 22 '17 at 05:43
  • Is there anyway to control or disable this internal behavior of Chrome? – drerD Jul 07 '17 at 01:09