如何在Chrome驱动程序中使用Selenium预加载的图像?

如何在Chrome驱动程序中使用Selenium预加载的图像?

问题描述:

我在Python中使用Selenium框架,我试图通过在Python代码中重新使用Chrome webdriver中预加载的图像来实现自动化来节省带宽。我能够访问图像的源URL,到目前为止,我只是用请求重新装载它们。我确定普通的浏览器可以将图像保存到硬盘上的临时位置,我想Chrome驱动程序也是一样的。如何使用Selenium来访问这些文件,因为我知道src?

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?

您应该重用您的chrome配置文件,类似于:

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)

关于路径,这可以通过在你的chrome中使用 chrome:// version / 来完成,的设置有你的路径,这将重用你的chrome配置文件,如果它是默认目录,你可能需要从路径中删除最后一个目录。

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.

如果您希望自己的个人资料不与您的Chrome浏览器相关联,那么您可以指定任何你想要的路径,硒将为你创建它。

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.