-1

I've looked through similar questions, and they all suggest using urllib/requests or taking a screenshot.

Downloading the image with urllib/requests presents a different user agent and doesn't come with my session cookies. Taking a screenshot limits the resolution to what is shown on my screen.

Is there a way to essentially right-click and "copy image" so I can save the image to file without redownloading the image or settling for the scale that is shown on the screen?

Edit:

JavaScript simulate right click through code is the closest thing to a legitimate answer within the thread of which this was suggested to be a duplicate. However, it is javascript, and not python.

The screenshot answer, which appears to be the de facto solution, doesn't fit my needs because the image I'm trying to capture is of higher resolution than is the region of the screen it is scaled to encompass.

I used the element.screenshot_as_png technique on a google images search result, and the image which "save as" returns with resolution 2932x718, becomes a cropped screenshot of dimensions 433x106. I want the full resolution image without downloading it again (which I admit "save as" does).

Since I can "copy image" without downloading the image again, I should be able to do something of the sort through selenium (preferably without requiring that I learn javascript). Maybe this can be done with selenium-wire(?), although I'd rather as few dependencies as possible.

  • Possible duplicate: https://stackoverflow.com/questions/17361742/download-image-with-selenium-python – koder613 Oct 04 '20 at 19:49

2 Answers2

0

This is a windows solutions that can right click and go down 8 spots to the copy image.

import win32com.client as comclt
from time import sleep
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(img).context_click().perform()
for i in range(8):
    wsh.SendKeys("{DOWN}")
    time.sleep(1)
wsh.SendKeys("{ENTER}")
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • I appreciate your answer, but wasn't able to get it to work. https://stackoverflow.com/questions/45789188/how-to-navigate-context-menus-selenium-python This thread would appear to indicate that you cannot send_keys to the context menu. I tried key_down and key_up in hopes that it might be different, however that would appear not to be the case. – SelfTaughtProgrammer Oct 05 '20 at 05:13
  • @SelfTaughtProgrammer you can't use selenium to do it but I think pyautogui or even this could copy image. – Arundeep Chohan Oct 05 '20 at 06:52
0

It's rather inelegant, but functionally does what I'm looking for:

#once you've found the src of the image
driver.get("about:cache?storage=disk") #navigate to Firefox cache
#find the cache for your file
driver.find_element_by_link_text("src/url/of/image.png").click() 
cache_data=driver.find_element_by_xpath("/html/body/pre")
#parse the hex code into bytes
data=cache_data.text
temp=data.split("\n")
data0=[]
for line in temp:
    data0+=line.split("  ")[1:17]
data1=[]
for char in data0:
    try:
        data1+=[int(char,16)]
    except:
        pass

data2=bytes(data1)
#save to file
with open(r"c\:filepath.png","wb") as file:
        file.write(data2)